Guest User

Untitled

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