| 1 |
/** |
| 2 |
* Woo Cart Cross Sells widget behavior. |
| 3 |
* |
| 4 |
* Initializes a Slick carousel for cross-sell products when enabled. |
| 5 |
*/ |
| 6 |
(function ($) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
window.KACartCrossSells = window.KACartCrossSells || (() => { |
| 10 |
const initSlider = (container) => { |
| 11 |
const products = container.querySelector('.products'); |
| 12 |
if (!products) return; |
| 13 |
|
| 14 |
const cols = parseInt(container.dataset.cols || '4', 10); |
| 15 |
const colsTablet = parseInt(container.dataset.colsTablet || Math.max(2, Math.min(cols, 3)), 10); |
| 16 |
const colsMobile = parseInt(container.dataset.colsMobile || '1', 10); |
| 17 |
const showArrows = container.dataset.arrows === '1'; |
| 18 |
const showDots = container.dataset.dots === '1'; |
| 19 |
const loop = container.dataset.loop === '1'; |
| 20 |
const autoplay = container.dataset.autoplay === '1'; |
| 21 |
const autoplaySpeed = parseInt(container.dataset.autoplaySpeed || '4000', 10); |
| 22 |
|
| 23 |
if (typeof $.fn.slick === "function") { |
| 24 |
const $el = $(products); |
| 25 |
if ($el.hasClass('slick-initialized')) { |
| 26 |
$el.slick('unslick'); |
| 27 |
} |
| 28 |
$el.slick({ |
| 29 |
slidesToShow: cols, |
| 30 |
slidesToScroll: 1, |
| 31 |
infinite: loop, |
| 32 |
dots: showDots, |
| 33 |
arrows: showArrows, |
| 34 |
autoplay, |
| 35 |
autoplaySpeed, |
| 36 |
responsive: [ |
| 37 |
{ breakpoint: 1024, settings: { slidesToShow: colsTablet } }, |
| 38 |
{ breakpoint: 768, settings: { slidesToShow: colsMobile } }, |
| 39 |
], |
| 40 |
}); |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
const init = () => { |
| 45 |
document.querySelectorAll('.ka-woo-cart-cross-sells').forEach((wrap) => { |
| 46 |
initSlider(wrap); |
| 47 |
}); |
| 48 |
}; |
| 49 |
|
| 50 |
return { init, initSlider }; |
| 51 |
})(); |
| 52 |
|
| 53 |
const initInScope = ($scope) => { |
| 54 |
const root = $scope && $scope[0] ? $scope[0] : document; |
| 55 |
root.querySelectorAll(".ka-woo-cart-cross-sells").forEach((wrap) => { |
| 56 |
// initSlider lives in the closure above; calling it by name here threw |
| 57 |
// "ReferenceError: initSlider is not defined" on every Elementor |
| 58 |
// element_ready - in the editor and after any AJAX cart refresh. |
| 59 |
window.KACartCrossSells.initSlider(wrap); |
| 60 |
}); |
| 61 |
}; |
| 62 |
|
| 63 |
document.addEventListener("DOMContentLoaded", () => window.KACartCrossSells.init()); |
| 64 |
|
| 65 |
$(window).on("elementor/frontend/init", function () { |
| 66 |
elementorFrontend.hooks.addAction( |
| 67 |
"frontend/element_ready/woo_cart_cross_sells.default", |
| 68 |
function ($scope) { |
| 69 |
initInScope($scope); |
| 70 |
} |
| 71 |
); |
| 72 |
}); |
| 73 |
})(jQuery); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|