Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 KB | None | 0 0
  1. jQuery(document).ready(function($) {
  2.  
  3. cart_improvement_functions();
  4. cart_dropdown_improvement();
  5. track_ajax_add_to_cart();
  6.  
  7. if(jQuery.fn.avia_sc_slider)
  8. {
  9. jQuery(".shop_slider_yes ul").avia_sc_slider({appendControlls:false, group:true, slide:'.product', arrowControll: true, autorotationInterval:'parent'});
  10. }
  11.  
  12.  
  13. //make woocommerce 3.6 gallery search icon clickable and open lightbox
  14. jQuery( 'body.single-product' ).on( 'click', '.single-product-main-image .avia-wc-30-product-gallery-lightbox', function( e ){
  15. e.preventDefault();
  16. var clicked = $(this), container = clicked.parents('.single-product-main-image');
  17. var img = container.find('.flex-active-slide a.lightbox-added').eq(0);
  18.  
  19. //if no gallery is used we need to find the original image size differently
  20. if(img.length == 0)
  21. {
  22. img = container.find('a.lightbox-added').eq(0);
  23. }
  24.  
  25. img.trigger('click');
  26. });
  27.  
  28.  
  29. product_add_to_cart_click();
  30.  
  31.  
  32. function avia_apply_quant_btn()
  33. {
  34. jQuery(".quantity input[type=number]").each(function() {
  35. var number = $(this),
  36. max = parseFloat( number.attr( 'max' ) ),
  37. min = parseFloat( number.attr( 'min' ) ),
  38. step = parseInt( number.attr( 'step' ), 10 ),
  39. newNum = jQuery(jQuery('<div />').append(number.clone(true)).html().replace('number','text')).insertAfter(number);
  40. number.remove();
  41.  
  42. setTimeout(function(){
  43. if(newNum.next('.plus').length === 0) {
  44. var minus = jQuery('<input type="button" value="-" class="minus">').insertBefore(newNum),
  45. plus = jQuery('<input type="button" value="+" class="plus">').insertAfter(newNum);
  46.  
  47. minus.on('click', function(){
  48. var the_val = parseInt( newNum.val(), 10 ) - step;
  49. the_val = the_val < 0 ? 0 : the_val;
  50. the_val = the_val < min ? min : the_val;
  51. newNum.val(the_val).trigger("change");
  52. });
  53. plus.on('click', function(){
  54. var the_val = parseInt( newNum.val(), 10 ) + step;
  55. the_val = the_val > max ? max : the_val;
  56. newNum.val(the_val).trigger("change");
  57.  
  58. });
  59. }
  60. },10);
  61.  
  62. });
  63. }
  64.  
  65. avia_apply_quant_btn();
  66.  
  67. //if the cart gets updated via ajax (woocommerce 2.6 and higher) we need to re apply the +/- buttons
  68. $( document ).on( 'updated_cart_totals', avia_apply_quant_btn );
  69.  
  70. setTimeout(first_load_amount, 10);
  71. $('body').on( 'added_to_cart', update_cart_dropdown );
  72. $('body').on( 'av_cart_changed', avia_cart_toggle );
  73. $('body').on( 'wc_fragments_refreshed', avia_cart_dropdown_changed );
  74. $(document).on( 'ready', avia_cart_toggle );
  75.  
  76.  
  77. // small fix for the hover menu for woocommerce sort buttons since it does no seem to work on mobile devices.
  78. // even if no event is actually bound the css dropdown works. if the binding is removed dropdown does no longer work.
  79. jQuery('.avia_mobile .sort-param').on('touchstart', function(){});
  80.  
  81. });
  82.  
  83. function avia_cart_toggle() {
  84. var cart_counter = jQuery('.cart_dropdown .av-cart-counter'),
  85. menu_cart = jQuery('.cart_dropdown'),
  86. counter = 0;
  87.  
  88. menu_cart.find('.cart_list li .quantity').each(function(){
  89. counter += parseInt(jQuery(this).text(),10);
  90. });
  91.  
  92. console.log(counter);
  93.  
  94. if( counter === 0 ) {
  95. menu_cart.hide();
  96. } else {
  97. menu_cart.show();
  98. }
  99. }
  100.  
  101.  
  102. /**
  103. * The ajax cart dropdown counter needs to be changed on cart page when user removes items or changes amount -
  104. * we have to check for changed amount of products in cart dropdown to update the cart counter
  105. * (reacts on remove items and changes to quantity)
  106. */
  107. function avia_cart_dropdown_changed()
  108. {
  109. var the_html = jQuery('html'),
  110. cart = jQuery('body').is('.woocommerce-cart'),
  111. cart_counter = jQuery('.cart_dropdown .av-cart-counter'),
  112. menu_cart = jQuery('.cart_dropdown'),
  113. counter = 0;
  114.  
  115. if( ! cart )
  116. {
  117. return;
  118. }
  119.  
  120. menu_cart.find('.cart_list li .quantity').each(function(){
  121. counter += parseInt(jQuery(this).text(),10);
  122. });
  123.  
  124. if( counter === 0 )
  125. {
  126. cart_counter.removeClass('av-active-counter').text(counter);
  127. setTimeout( function() { the_html.removeClass('html_visible_cart'); }, 200);
  128.  
  129. }
  130. else if( (cart_counter.length > 0 ) && ( counter > 0 ) )
  131. {
  132. setTimeout(function(){
  133. cart_counter.addClass('av-active-counter').text(counter);
  134. the_html.addClass('html_visible_cart');
  135. jQuery('body').trigger('av_cart_changed');
  136. }, 10);
  137. }
  138.  
  139. return;
  140. }
  141.  
  142.  
  143. //updates the shopping cart in the sidebar, hooks into the added_to_cart event whcih is triggered by woocommerce
  144. function update_cart_dropdown(event)
  145. {
  146. var the_html = jQuery('html'),
  147. menu_cart = jQuery('.cart_dropdown'),
  148. cart_counter = jQuery('.cart_dropdown .av-cart-counter'),
  149. empty = menu_cart.find('.empty'),
  150. msg_success = menu_cart.data('success'),
  151. product = jQuery.extend({name:"Product", price:"", image:""}, avia_clicked_product),
  152. counter = 0;
  153.  
  154. // trigger changed in WC 3.0.0 - must check for event explecit
  155. if( (empty.length > 0) && ( 'undefined' !== typeof event ) )
  156. {
  157. the_html.addClass('html_visible_cart');
  158. }
  159.  
  160. if(typeof event !== 'undefined')
  161. {
  162. var header = jQuery('.html_header_sticky #header_main .cart_dropdown_first, .html_header_sidebar #header_main .cart_dropdown_first'),
  163. oldTemplates = jQuery('.added_to_cart_notification').trigger('avia_hide'),
  164. template = jQuery("<div class='added_to_cart_notification'><span class='avia-arrow'></span><div class='added-product-text'><strong>\"" + product.name +"\"</strong> "+ msg_success+ "</div> " + product.image +"</div>").css( 'opacity', 0 );
  165.  
  166. if(!header.length) header = 'body';
  167.  
  168. template.on('mouseenter avia_hide', function()
  169. {
  170. template.animate({opacity:0, top: parseInt(template.css('top'), 10) + 15 }, function()
  171. {
  172. template.remove();
  173. });
  174.  
  175. }).appendTo(header).animate({opacity:1},500);
  176.  
  177. setTimeout(function(){ template.trigger('avia_hide'); }, 2500);
  178. }
  179.  
  180. // with WC 3.0.0 DOM is not ready - wrong calculation of counter (last element missing)
  181. setTimeout(function(){
  182. menu_cart.find('.cart_list li .quantity').each(function(){
  183. counter += parseInt(jQuery(this).text(),10);
  184. });
  185.  
  186. if( (cart_counter.length > 0) && (counter > 0) )
  187. {
  188. cart_counter.removeClass('av-active-counter');
  189. setTimeout(function(){
  190. cart_counter.addClass('av-active-counter').text(counter);
  191. jQuery('body').trigger('av_cart_changed');
  192. }, 10);
  193. }
  194. }, 300 );
  195. }
  196.  
  197.  
  198. var avia_clicked_product = {};
  199. function track_ajax_add_to_cart()
  200. {
  201. jQuery('body').on('click','.add_to_cart_button', function(e)
  202. {
  203.  
  204. var productContainer = jQuery(this).parents('.product').eq(0), product = {};
  205. product.name = productContainer.find('.woocommerce-loop-product__title').text();
  206. product.image = productContainer.find('.thumbnail_container img');
  207. product.price = productContainer.find('.price .amount').last().text();
  208.  
  209. //lower than woocommerce 3.0.0
  210. if(product.name === "") product.name = productContainer.find('.inner_product_header h3').text();
  211.  
  212.  
  213. /*fallbacks*/
  214. if(productContainer.length === 0)
  215. {
  216. productContainer = jQuery(this);
  217. product.name = productContainer.find('.av-cart-update-title').text();
  218. product.image = productContainer.find('.av-cart-update-image');
  219. product.price = productContainer.find('.av-cart-update-price').text();
  220. }
  221.  
  222. if(product.image.length)
  223. {
  224. product.image = "<img class='added-product-image' src='" + product.image.get(0).src + "' title='' alt='' />";
  225. }
  226. else
  227. {
  228. product.image = "";
  229. }
  230.  
  231. avia_clicked_product = product;
  232. });
  233. }
  234.  
  235.  
  236. //function that pre fills the amount value of the cart
  237. function first_load_amount()
  238. {
  239. var counter = 0,
  240. limit = 15,
  241. ms = 500,
  242. check = function()
  243. {
  244. var new_total = jQuery('.cart_dropdown .dropdown_widget_cart:eq(0) .total .amount');
  245.  
  246. if(new_total.length)
  247. {
  248. update_cart_dropdown();
  249. }
  250. else
  251. {
  252. counter++;
  253. if(counter < limit)
  254. {
  255. setTimeout(check, ms);
  256. }
  257. }
  258. };
  259.  
  260. check();
  261.  
  262. //display the cart for a short moment on page load if a product was added but no notice was delivered (eg template builder page)
  263. if (jQuery('.av-display-cart-on-load').length && jQuery('.woocommerce-message').length === 0)
  264. {
  265. var dropdown = jQuery('.cart_dropdown');
  266. setTimeout( function(){dropdown.trigger('mouseenter'); }, 500);
  267. setTimeout( function(){dropdown.trigger('mouseleave'); }, 2500);
  268. }
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275. function product_add_to_cart_click()
  276. {
  277. var jbody = jQuery('body'),
  278. catalogue = jQuery('.av-catalogue-item'),
  279. loader = false;
  280.  
  281. if(catalogue.length) loader = jQuery.avia_utilities.loading();
  282.  
  283. jbody.on('click', '.add_to_cart_button', function(e)
  284. {
  285. var button = jQuery(this);
  286. button.parents('.product:eq(0)').addClass('adding-to-cart-loading').removeClass('added-to-cart-check');
  287.  
  288. if(button.is('.av-catalogue-item'))
  289. {
  290. loader.show();
  291. }
  292.  
  293. var $html = jQuery('html');
  294. if( ! $html.hasClass( 'html_visible_cart' ) )
  295. {
  296. $html.addClass('html_visible_cart');
  297. }
  298.  
  299. //e.preventDefault();
  300. });
  301.  
  302. jbody.on( 'added_to_cart', function()
  303. {
  304. jQuery('.adding-to-cart-loading').removeClass('adding-to-cart-loading').addClass('added-to-cart-check');
  305.  
  306. if(loader !== false)
  307. {
  308. loader.hide();
  309. }
  310. });
  311.  
  312. }
  313.  
  314.  
  315.  
  316. // little fixes and modifications to the dom
  317. function cart_improvement_functions()
  318. {
  319. //single products are added via ajax //doesnt work currently
  320. //jQuery('.summary .cart .button[type=submit]').addClass('add_to_cart_button product_type_simple');
  321.  
  322. //downloadable products are now added via ajax as well
  323. jQuery('.product_type_downloadable, .product_type_virtual').addClass('product_type_simple');
  324.  
  325. //clicking tabs dont activate smoothscrooling
  326. jQuery('.woocommerce-tabs .tabs a').addClass('no-scroll');
  327.  
  328. //connect thumbnails on single product page via lightbox
  329. jQuery('.single-product-main-image>.images a').attr('rel','product_images[grouped]');
  330.  
  331.  
  332.  
  333. }
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. //small function that improves shoping cart hover behaviour in the menu
  341. function cart_dropdown_improvement()
  342. {
  343. var dropdown = jQuery('.cart_dropdown'), subelement = dropdown.find('.dropdown_widget').css({display:'none', opacity:0});
  344.  
  345. dropdown.hover(
  346. function(){ subelement.css({display:'block'}).stop().animate({opacity:1}); },
  347. function(){ subelement.stop().animate({opacity:0}, function(){ subelement.css({display:'none'}); }); }
  348. );
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement