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(); } diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index cf23a13a3..d7dd4c168 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -424,20 +424,22 @@ class SmartButton implements SmartButtonInterface { add_action( $this->single_product_renderer_hook(), function () { + $product = wc_get_product(); + + if ( + is_a( $product, WC_Product::class ) + && ! $this->product_supports_payment( $product ) + ) { + + return; + } + $this->button_renderer( PayPalGateway::ID ); }, 31 ); } - add_action( - $this->pay_order_renderer_hook(), - function (): void { - $this->button_renderer( PayPalGateway::ID ); - $this->button_renderer( CardButtonGateway::ID ); - } - ); - $not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) && ! $this->settings->get( 'button_mini_cart_enabled' ); if ( @@ -464,27 +466,38 @@ class SmartButton implements SmartButtonInterface { ); } - add_action( - $this->checkout_button_renderer_hook(), - function (): void { - $this->button_renderer( PayPalGateway::ID ); - $this->button_renderer( CardButtonGateway::ID ); - } - ); + $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(), + 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 ); + } + ); - $this->button_renderer( PayPalGateway::ID ); - }, - 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( PayPalGateway::ID ); + }, + 20 + ); + } return true; } @@ -546,22 +559,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[ $gateway_id ] ) ) { - return; - } - // The wrapper is needed for the loading spinner, // otherwise jQuery block() prevents buttons rendering. echo '
'; @@ -940,7 +937,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, @@ -949,9 +948,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 )