From 5d43dfeb8bdc4376c845787691f97c1ae7f2f653 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Mon, 24 Jul 2023 08:35:23 +0100 Subject: [PATCH] Change method to add an event listener on single product page. Add check for price not found in DOM for single product page. --- .../ContextBootstrap/SingleProductBootstap.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js index 231460642..9627c0ca8 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js @@ -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; }