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 896a357c4..6d7508c68 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -336,22 +336,6 @@ class SmartButton implements SmartButtonInterface { */ private function render_button_wrapper_registrar(): bool { - $not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) && - ! $this->settings->get( 'button_cart_enabled' ); - if ( - is_cart() - && ! $not_enabled_on_cart - ) { - add_action( - $this->proceed_to_checkout_button_renderer_hook(), - array( - $this, - 'button_renderer', - ), - 20 - ); - } - $not_enabled_on_product_page = $this->settings->has( 'button_single_product_enabled' ) && ! $this->settings->get( 'button_single_product_enabled' ); if ( @@ -368,6 +352,26 @@ class SmartButton implements SmartButtonInterface { ); } + if ( $this->is_cart_price_total_zero() ) { + return false; + } + + $not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) && + ! $this->settings->get( 'button_cart_enabled' ); + if ( + is_cart() + && ! $not_enabled_on_cart + ) { + add_action( + $this->proceed_to_checkout_button_renderer_hook(), + array( + $this, + 'button_renderer', + ), + 20 + ); + } + $not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) && ! $this->settings->get( 'button_mini_cart_enabled' ); if ( @@ -449,6 +453,7 @@ class SmartButton implements SmartButtonInterface { || ! $product->is_in_stock() ) ) { + return; } @@ -586,7 +591,7 @@ class SmartButton implements SmartButtonInterface { return; } - // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch + // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch $label = 'checkout' === $this->context() ? apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) ) : __( 'Pay for order', 'woocommerce' ); printf( @@ -1106,4 +1111,14 @@ class SmartButton implements SmartButtonInterface { */ return (string) apply_filters( 'woocommerce_paypal_payments_single_product_renderer_hook', 'woocommerce_single_product_summary' ); } + + /** + * Check if cart product price total is 0. + * + * @return bool true if is 0, otherwise false. + */ + protected function is_cart_price_total_zero(): bool { + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison + return WC()->cart->get_cart_contents_total() == 0; + } }