From 164b74fdcba08b31c6543b224752bf9fde9eb7fc Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 17 Feb 2022 20:01:47 +0400 Subject: [PATCH 1/4] Check if price total is zero before showing buttons --- .../ppcp-button/src/Assets/SmartButton.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 667560dbc..1ee46a2ae 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -331,6 +331,7 @@ class SmartButton implements SmartButtonInterface { if ( is_cart() && ! $not_enabled_on_cart + && ! $this->is_cart_price_total_zero() ) { add_action( $this->proceed_to_checkout_button_renderer_hook(), @@ -347,6 +348,7 @@ 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(), @@ -362,6 +364,7 @@ class SmartButton implements SmartButtonInterface { ! $this->settings->get( 'button_mini_cart_enabled' ); if ( ! $not_enabled_on_minicart + && ! $this->is_cart_price_total_zero() ) { add_action( $this->mini_cart_button_renderer_hook(), @@ -439,6 +442,7 @@ class SmartButton implements SmartButtonInterface { || ! $product->is_in_stock() ) ) { + return; } @@ -568,7 +572,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( @@ -1070,4 +1074,30 @@ class SmartButton implements SmartButtonInterface { private function single_product_renderer_hook(): string { 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 { + $cart_total = WC()->cart->get_cart_contents_total(); + return ! ( $cart_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(); + $product_price = $product ? $product->get_price() : 0; + + return ! ( (float) $product_price > 0 ); + } } From efca8ba27dde17fea3dc027f732bb2ac5306e635 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 18 Feb 2022 17:56:33 +0400 Subject: [PATCH 2/4] use == comparison --- modules/ppcp-button/src/Assets/SmartButton.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 1ee46a2ae..7eb81e440 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -1081,8 +1081,8 @@ class SmartButton implements SmartButtonInterface { * @return bool true if is 0, otherwise false. */ protected function is_cart_price_total_zero(): bool { - $cart_total = WC()->cart->get_cart_contents_total(); - return ! ( $cart_total > 0 ); + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison + return WC()->cart->get_cart_contents_total() == 0; } /** @@ -1095,9 +1095,9 @@ class SmartButton implements SmartButtonInterface { return false; } - $product = wc_get_product(); - $product_price = $product ? $product->get_price() : 0; + $product = wc_get_product(); - return ! ( (float) $product_price > 0 ); + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison + return $product && $product->get_price() == 0; } } From 6de1aa2ae8f24966b4becea855ccb172d0c0ad52 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 18 Feb 2022 18:06:55 +0400 Subject: [PATCH 3/4] early return if cart is empty --- .../ppcp-button/src/Assets/SmartButton.php | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 7eb81e440..d4d887626 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -326,23 +326,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 - && ! $this->is_cart_price_total_zero() - ) { - 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 ( @@ -360,11 +343,30 @@ 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 ( ! $not_enabled_on_minicart - && ! $this->is_cart_price_total_zero() ) { add_action( $this->mini_cart_button_renderer_hook(), From 33ad9f6d9140471c89a83be25eb1f8a31145d604 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Wed, 2 Mar 2022 12:49:21 +0400 Subject: [PATCH 4/4] Check the price amount with JS to fix variable products --- .../ContextBootstrap/SingleProductBootstap.js | 44 ++++++++++++++++--- .../ppcp-button/src/Assets/SmartButton.php | 17 ------- 2 files changed, 37 insertions(+), 24 deletions(-) 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; - } }