Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.3 kB
2
Indexable
Never
const variationsSelects = document.querySelectorAll('table.variations select');
const quantityInput = document.querySelectorAll('.quantity input');
const weightInput = document.querySelector('#weight_needed');
const quantityDisplay = document.querySelector('.quantity .input-text');

function toEnDigit(s) {
  return s?.replace(/[\u0660-\u0669\u06f0-\u06f9]/g,    
      function(a) { return a.charCodeAt(0) & 0xf }    
  )
}

function calculatePrice() {
  setTimeout(() => {
    const productPrice = document.querySelector('span.product_price .amount');
    const price = toEnDigit(document.querySelector('form.variations_form.cart.wd-reset-side-lg.wd-reset-bottom-md.wd-label-top-md.variation-swatch-selected bdi')?.innerText?.replace(',', '')?.replace(' ', '')?.replace('تومان', ''));
    const weight = weightInput?.value?.replace(' کیلوگرم', '');
    const quantity = toEnDigit(quantityDisplay.value);
    productPrice.innerText = `${((Number(weight) * Number(price)) * Number(quantity)).toLocaleString('fa')} ${'تومان'}`;
  }, 200);
}

[...variationsSelects, ...quantityInput, weightInput, quantityDisplay].forEach((item) => {
  item.addEventListener('click', calculatePrice);
  item.addEventListener('change', calculatePrice);
});