Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- window.addEventListener('load', () => {
- const products = document.querySelectorAll('[data-pf-type="ProductBox"]');
- for (let i = 0; i < products.length; i++) {
- const productSwatches = products[i].querySelector('[data-pf-type="ProductVariantSwatches"]');
- const qytBtns = products[i].querySelectorAll('[data-pf-type="QuantityButton"]');
- const quantityField = products[i].querySelector('[data-pf-type=QuantityField]');
- const priceDOM = products[i].querySelector('[data-product-type=price]');
- const comparePriceDOM = products[i].querySelector('[data-product-type=compare_at_price]');
- const storeCurrency = `{{ cart.currency.symbol }}`;
- const handleChange = () => {
- setTimeout(() => {
- const inputDOM = quantityField.closest('[data-pf-type=ProductBox]')?.querySelector('input[type=hidden][data-product-id]');
- const variantID = parseInt(inputDOM.value);
- const quantity = parseInt(quantityField.value);
- const productID = inputDOM.getAttribute('data-product-id');
- if (variantID && quantity) {
- const variant = __pageflyProducts[productID].variants.filter(variant => variant.id === variantID)[0];
- if (priceDOM) {
- priceDOM.innerHTML = `${storeCurrency}${((variant.price * quantity) / 100).toFixed(2)}`;
- }
- if (comparePriceDOM) {
- comparePriceDOM.innerHTML = `${storeCurrency}${((variant.compare_at_price * quantity) / 100).toFixed(2)}`;
- }
- }
- }, 0);
- };
- quantityField.addEventListener('change', handleChange);
- productSwatches.addEventListener('change', handleChange);
- qytBtns.forEach(btn => btn.addEventListener('click', handleChange));
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement