Advertisement
Guest User

Untitled

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