Advertisement
Guest User

Untitled

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