diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js index fc9bc6ca6..33658d9c2 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js @@ -9,21 +9,51 @@ class SingleProductBootstap { this.messages = messages; } - init() { + + handleChange() { if (!this.shouldRender()) { - this.renderer.hideButtons(this.gateway.hosted_fields.wrapper); + this.renderer.hideButtons(this.gateway.hosted_fields.wrapper); + this.renderer.hideButtons(this.gateway.button.wrapper); return; } this.render(); } - shouldRender() { - if (document.querySelector('form.cart') === null) { - return false; + init() { + + document.querySelector('form.cart').addEventListener('change', this.handleChange.bind(this)) + + if (!this.shouldRender()) { + this.renderer.hideButtons(this.gateway.hosted_fields.wrapper); + return; } - return true; + this.render(); + + } + + shouldRender() { + + return document.querySelector('form.cart') !== null && !this.priceAmountIsZero(); + + } + + priceAmountIsZero() { + + let priceText = "0"; + if (document.querySelector('form.cart ins .woocommerce-Price-amount')) { + priceText = document.querySelector('form.cart ins .woocommerce-Price-amount').innerText; + } + else if (document.querySelector('form.cart .woocommerce-Price-amount')) { + priceText = document.querySelector('form.cart .woocommerce-Price-amount').innerText; + } + else if (document.querySelector('.woocommerce-Price-amount')) { + priceText = document.querySelector('.woocommerce-Price-amount').innerText; + } + const amount = parseFloat(priceText.replace(/([^\d,\.\s]*)/g, '')); + return amount === 0; + } render() { @@ -62,4 +92,4 @@ class SingleProductBootstap { } } -export default SingleProductBootstap; \ No newline at end of file +export default SingleProductBootstap; diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index d4d887626..38754bd0d 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -331,7 +331,6 @@ class SmartButton implements SmartButtonInterface { if ( ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) && ! $not_enabled_on_product_page - && ! $this->is_product_price_total_zero() ) { add_action( $this->single_product_renderer_hook(), @@ -1086,20 +1085,4 @@ class SmartButton implements SmartButtonInterface { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison return WC()->cart->get_cart_contents_total() == 0; } - - /** - * Check if current product price total is 0. - * - * @return bool true if is 0, otherwise false. - */ - protected function is_product_price_total_zero(): bool { - if ( ! is_product() ) { - return false; - } - - $product = wc_get_product(); - - // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison - return $product && $product->get_price() == 0; - } }