Change method to add an event listener on single product page.

Add check for price not found in DOM for single product page.
This commit is contained in:
Pedro Silva 2023-07-24 08:35:23 +01:00
parent 0672ca4950
commit 5d43dfeb8b
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3

View file

@ -51,7 +51,7 @@ class SingleProductBootstap {
return;
}
form.addEventListener('change', () => {
jQuery(document).on('change', this.formSelector, () => {
this.handleChange();
setTimeout(() => { // Wait for the DOM to be fully updated
@ -91,7 +91,7 @@ class SingleProductBootstap {
&& ((null === addToCartButton) || !addToCartButton.classList.contains('disabled'));
}
priceAmount() {
priceAmount(returnOnUndefined = 0) {
const priceText = [
() => document.querySelector('form.cart ins .woocommerce-Price-amount')?.innerText,
() => document.querySelector('form.cart .woocommerce-Price-amount')?.innerText,
@ -110,6 +110,10 @@ class SingleProductBootstap {
},
].map(f => f()).find(val => val);
if (typeof priceText === 'undefined') {
return returnOnUndefined;
}
if (!priceText) {
return 0;
}
@ -118,7 +122,13 @@ class SingleProductBootstap {
}
priceAmountIsZero() {
const price = this.priceAmount();
const price = this.priceAmount(-1);
// if we can't find the price in the DOM we want to return true so the button is visible.
if (price === -1) {
return false;
}
return !price || price === 0;
}