← All changes
|
includes/widgets/Woo_Cart_Cross_Sells/script.js
+17
-6
51.1.76
→
51.1.87
View file →
| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | const initSlider = (container) => { |
| 11 | 11 | const products = container.querySelector('.products'); |
| 12 | 12 | if (!products) return; |
| 13 | 13 | |
| 14 | + const count = products.querySelectorAll('li.product').length; | |
| 14 | 15 | const cols = parseInt(container.dataset.cols || '4', 10); |
| 15 | 16 | const colsTablet = parseInt(container.dataset.colsTablet || Math.max(2, Math.min(cols, 3)), 10); |
| 16 | 17 | const colsMobile = parseInt(container.dataset.colsMobile || '1', 10); |
| 17 | 18 | const showArrows = container.dataset.arrows === '1'; |
| @@ -18,9 +19,16 @@ | ||
| 18 | 19 | const showDots = container.dataset.dots === '1'; |
| 19 | 20 | const loop = container.dataset.loop === '1'; |
| 20 | 21 | const autoplay = container.dataset.autoplay === '1'; |
| 21 | 22 | const autoplaySpeed = parseInt(container.dataset.autoplaySpeed || '4000', 10); |
| 23 | + const slidesDesktop = Math.max(1, Math.min(cols, count)); | |
| 24 | + const slidesTablet = Math.max(1, Math.min(colsTablet, count)); | |
| 25 | + const slidesMobile = Math.max(1, Math.min(colsMobile, count)); | |
| 22 | 26 | |
| 27 | + if (count <= 1) { | |
| 28 | + return; | |
| 29 | + } | |
| 30 | + | |
| 23 | 31 | if (typeof $.fn.slick === "function") { |
| 24 | 32 | const $el = $(products); |
| 25 | 33 | if ($el.hasClass('slick-initialized')) { |
| 26 | 34 | $el.slick('unslick'); |
| @@ -25,18 +33,18 @@ | ||
| 25 | 33 | if ($el.hasClass('slick-initialized')) { |
| 26 | 34 | $el.slick('unslick'); |
| 27 | 35 | } |
| 28 | 36 | $el.slick({ |
| 29 | - slidesToShow: cols, | |
| 37 | + slidesToShow: slidesDesktop, | |
| 30 | 38 | slidesToScroll: 1, |
| 31 | - infinite: loop, | |
| 39 | + infinite: loop && count > slidesDesktop, | |
| 32 | 40 | dots: showDots, |
| 33 | 41 | arrows: showArrows, |
| 34 | 42 | autoplay, |
| 35 | 43 | autoplaySpeed, |
| 36 | 44 | responsive: [ |
| 37 | - { breakpoint: 1024, settings: { slidesToShow: colsTablet } }, | |
| 38 | - { breakpoint: 768, settings: { slidesToShow: colsMobile } }, | |
| 45 | + { breakpoint: 1024, settings: { slidesToShow: slidesTablet } }, | |
| 46 | + { breakpoint: 768, settings: { slidesToShow: slidesMobile } }, | |
| 39 | 47 | ], |
| 40 | 48 | }); |
| 41 | 49 | } |
| 42 | 50 | }; |
| @@ -46,15 +54,18 @@ | ||
| 46 | 54 | initSlider(wrap); |
| 47 | 55 | }); |
| 48 | 56 | }; |
| 49 | 57 | |
| 50 | - return { init }; | |
| 58 | + return { init, initSlider }; | |
| 51 | 59 | })(); |
| 52 | 60 | |
| 53 | 61 | const initInScope = ($scope) => { |
| 54 | 62 | const root = $scope && $scope[0] ? $scope[0] : document; |
| 55 | 63 | root.querySelectorAll(".ka-woo-cart-cross-sells").forEach((wrap) => { |
| 56 | - initSlider(wrap); | |
| 64 | + // initSlider lives in the closure above; calling it by name here threw | |
| 65 | + // "ReferenceError: initSlider is not defined" on every Elementor | |
| 66 | + // element_ready - in the editor and after any AJAX cart refresh. | |
| 67 | + window.KACartCrossSells.initSlider(wrap); | |
| 57 | 68 | }); |
| 58 | 69 | }; |
| 59 | 70 | |
| 60 | 71 | document.addEventListener("DOMContentLoaded", () => window.KACartCrossSells.init()); |