From 036e7d0c33c5992df8b31c86f61fc2124ed3c8cc Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Mon, 27 Jun 2022 15:47:21 +0400 Subject: [PATCH 1/4] Fix button_renderer logic & the behavior for single products --- .../ppcp-button/src/Assets/SmartButton.php | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index d6b772fbf..64c3b6bd8 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -411,16 +411,23 @@ class SmartButton implements SmartButtonInterface { ) { add_action( $this->single_product_renderer_hook(), - array( - $this, - 'button_renderer', - ), + function () { + $product = wc_get_product(); + + if ( + is_a( $product, WC_Product::class ) + && ! $this->product_supports_payment( $product ) + ) { + + return; + } + + $this->button_renderer(); + }, 31 ); } - add_action( $this->pay_order_renderer_hook(), array( $this, 'button_renderer' ), 10 ); - $not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) && ! $this->settings->get( 'button_mini_cart_enabled' ); if ( @@ -447,21 +454,26 @@ class SmartButton implements SmartButtonInterface { ); } - add_action( $this->checkout_button_renderer_hook(), array( $this, 'button_renderer' ), 10 ); + $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); - $not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) && - ! $this->settings->get( 'button_cart_enabled' ); - add_action( - $this->proceed_to_checkout_button_renderer_hook(), - function() use ( $not_enabled_on_cart ) { - if ( ! is_cart() || $not_enabled_on_cart || $this->is_free_trial_cart() || $this->is_cart_price_total_zero() ) { - return; - } + if ( isset( $available_gateways['ppcp-gateway'] ) ) { + add_action( $this->pay_order_renderer_hook(), array( $this, 'button_renderer' ), 10 ); + add_action( $this->checkout_button_renderer_hook(), array( $this, 'button_renderer' ), 10 ); - $this->button_renderer(); - }, - 20 - ); + $not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) && + ! $this->settings->get( 'button_cart_enabled' ); + add_action( + $this->proceed_to_checkout_button_renderer_hook(), + function() use ( $not_enabled_on_cart ) { + if ( ! is_cart() || $not_enabled_on_cart || $this->is_free_trial_cart() || $this->is_cart_price_total_zero() ) { + return; + } + + $this->button_renderer(); + }, + 20 + ); + } return true; } @@ -521,22 +533,6 @@ class SmartButton implements SmartButtonInterface { return; } - $product = wc_get_product(); - - if ( - ! is_checkout() && is_a( $product, WC_Product::class ) - && ! $this->product_supports_payment( $product ) - ) { - - return; - } - - $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); - - if ( ! isset( $available_gateways['ppcp-gateway'] ) ) { - return; - } - // The wrapper is needed for the loading spinner, // otherwise jQuery block() prevents buttons rendering. echo '
'; @@ -843,7 +839,7 @@ class SmartButton implements SmartButtonInterface { 'messages' => $this->message_values(), 'labels' => array( 'error' => array( - 'generic' => __( + 'generic' => __( 'Something went wrong. Please try again or choose another payment source.', 'woocommerce-paypal-payments' ), From b15c03d4181b6af88e3376e5dd93e808996d7271 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Mon, 4 Jul 2022 17:30:29 +0400 Subject: [PATCH 2/4] Fix the intent value for single products. --- modules/ppcp-button/src/Assets/SmartButton.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 64c3b6bd8..37a0955bf 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -886,7 +886,9 @@ class SmartButton implements SmartButtonInterface { * @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException If a setting was not found. */ private function url(): string { - $intent = ( $this->settings->has( 'intent' ) ) ? $this->settings->get( 'intent' ) : 'capture'; + $intent = ( $this->settings->has( 'intent' ) ) ? $this->settings->get( 'intent' ) : 'capture'; + $product_intent = $this->subscription_helper->current_product_is_subscription() ? 'authorize' : $intent; + $other_context_intent = $this->subscription_helper->cart_contains_subscription() ? 'authorize' : $intent; $params = array( 'client-id' => $this->client_id, @@ -895,9 +897,7 @@ class SmartButton implements SmartButtonInterface { 'components' => implode( ',', $this->components() ), 'vault' => $this->can_save_vault_token() ? 'true' : 'false', 'commit' => is_checkout() ? 'true' : 'false', - 'intent' => ( $this->subscription_helper->cart_contains_subscription() || $this->subscription_helper->current_product_is_subscription() ) - ? 'authorize' - : $intent, + 'intent' => $this->context() === 'product' ? $product_intent : $other_context_intent, ); if ( $this->environment->current_environment_is( Environment::SANDBOX ) From 65caa42872c3e06ae56e7b1d3166bf7ad8d9124e Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 26 Jul 2022 19:11:07 +0400 Subject: [PATCH 3/4] Fix PHPCS problems --- .../ppcp-button/src/Assets/SmartButton.php | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index eb80fdada..ae3b319f0 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -434,7 +434,7 @@ class SmartButton implements SmartButtonInterface { return; } - $this->button_renderer( PayPalGateway::ID ); + $this->button_renderer( PayPalGateway::ID ); }, 31 ); @@ -469,20 +469,20 @@ class SmartButton implements SmartButtonInterface { $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); if ( isset( $available_gateways['ppcp-gateway'] ) ) { - add_action( - $this->pay_order_renderer_hook(), - function (): void { - $this->button_renderer(PayPalGateway::ID); - $this->button_renderer(CardButtonGateway::ID); - } - ); - add_action( - $this->checkout_button_renderer_hook(), - function (): void { - $this->button_renderer(PayPalGateway::ID); - $this->button_renderer(CardButtonGateway::ID); - } - ); + add_action( + $this->pay_order_renderer_hook(), + function (): void { + $this->button_renderer( PayPalGateway::ID ); + $this->button_renderer( CardButtonGateway::ID ); + } + ); + add_action( + $this->checkout_button_renderer_hook(), + function (): void { + $this->button_renderer( PayPalGateway::ID ); + $this->button_renderer( CardButtonGateway::ID ); + } + ); $not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) && ! $this->settings->get( 'button_cart_enabled' ); @@ -493,11 +493,11 @@ class SmartButton implements SmartButtonInterface { return; } - $this->button_renderer(PayPalGateway::ID); - }, - 20 - ); - } + $this->button_renderer( PayPalGateway::ID ); + }, + 20 + ); + } return true; } From 6b8014e5b1600a834809b6e2dca3ce1b5f035c98 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 28 Jul 2022 16:33:24 +0400 Subject: [PATCH 4/4] Fix the JS file --- modules/ppcp-button/resources/js/button.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-button/resources/js/button.js b/modules/ppcp-button/resources/js/button.js index 4ec22c7d7..03a38a3f7 100644 --- a/modules/ppcp-button/resources/js/button.js +++ b/modules/ppcp-button/resources/js/button.js @@ -112,7 +112,7 @@ const bootstrap = () => { PayPalCommerceGateway, renderer, messageRenderer, - );w + ); singleProductBootstrap.init(); }