Advertisement
ikai2

price qty pf

Aug 17th, 2022 (edited)
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('load', () => {
  2.     const products = document.querySelectorAll('[data-pf-type="ProductBox"]');
  3.     for (let i = 0; i < products.length; i++) {
  4.         const productSwatches = products[i].querySelector('[data-pf-type="ProductVariantSwatches"]');
  5.         const qytBtns = products[i].querySelectorAll('[data-pf-type="QuantityButton"]');
  6.         const quantityField = products[i].querySelector('[data-pf-type=QuantityField]');
  7.         const priceDOM = products[i].querySelector('[data-product-type=price]');
  8.         const comparePriceDOM = products[i].querySelector('[data-product-type=compare_at_price]');
  9.  
  10.         const storeCurrency = `{{ cart.currency.symbol }}`;
  11.  
  12.         const handleChange = () => {
  13.             setTimeout(() => {
  14.                 const inputDOM = quantityField.closest('[data-pf-type=ProductBox]')?.querySelector('input[type=hidden][data-product-id]');
  15.  
  16.                 const variantID = parseInt(inputDOM.value);
  17.                 const quantity = parseInt(quantityField.value);
  18.                 const productID = inputDOM.getAttribute('data-product-id');
  19.  
  20.                 if (variantID && quantity) {
  21.                     const variant = __pageflyProducts[productID].variants.filter(variant => variant.id === variantID)[0];
  22.                     if (priceDOM) {
  23.                         priceDOM.innerHTML = `${storeCurrency}${((variant.price * quantity) / 100).toFixed(2)}`;
  24.                     }
  25.                     if (comparePriceDOM) {
  26.                         comparePriceDOM.innerHTML = `${storeCurrency}${((variant.compare_at_price * quantity) / 100).toFixed(2)}`;
  27.                     }
  28.                 }
  29.             }, 0);
  30.         };
  31.  
  32.         quantityField.addEventListener('change', handleChange);
  33.         productSwatches.addEventListener('change', handleChange);
  34.         qytBtns.forEach(btn => btn.addEventListener('click', handleChange));
  35.     }
  36. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement