From fc3ba70d4be7f0dbede1f1eddcdec8ba16b81547 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 11 May 2022 18:16:50 +0300 Subject: [PATCH] Extract filter to a function --- .../ppcp-button/src/Assets/SmartButton.php | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index cce84e9ac..61a5c21ed 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Button\Assets; use Exception; use Psr\Log\LoggerInterface; +use WC_Product; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; @@ -524,9 +525,8 @@ class SmartButton implements SmartButtonInterface { $product = wc_get_product(); if ( - ! is_checkout() && is_a( $product, \WC_Product::class ) - && ( ! apply_filters( 'woocommerce_paypal_payments_product_supports_payment_request_button', ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(), $product ) - ) + ! is_checkout() && is_a( $product, WC_Product::class ) + && ! $this->product_supports_payment( $product ) ) { return; @@ -550,9 +550,11 @@ class SmartButton implements SmartButtonInterface { $product = wc_get_product(); if ( - ! is_checkout() && is_a( $product, \WC_Product::class ) - && ( ! apply_filters( 'woocommerce_paypal_payments_product_supports_payment_request_button', ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(), $product ) - ) + ! is_checkout() && is_a( $product, WC_Product::class ) + /** + * The filter returning true if PayPal buttons can be rendered, or false otherwise. + */ + && ! $this->product_supports_payment( $product ) ) { return; } @@ -576,7 +578,7 @@ class SmartButton implements SmartButtonInterface { } $placement = 'product'; $product = wc_get_product(); - $amount = ( is_a( $product, \WC_Product::class ) ) ? wc_get_price_including_tax( $product ) : 0; + $amount = ( is_a( $product, WC_Product::class ) ) ? wc_get_price_including_tax( $product ) : 0; $layout = $this->settings->has( 'message_product_layout' ) ? $this->settings->get( 'message_product_layout' ) : 'text'; $logo_type = $this->settings->has( 'message_product_logo' ) ? @@ -1235,6 +1237,24 @@ class SmartButton implements SmartButtonInterface { return WC()->cart->get_cart_contents_total() == 0; } + /** + * Checks if PayPal buttons/messages can be rendered for the given product. + * + * @param WC_Product $product The product. + * + * @return bool + */ + protected function product_supports_payment( WC_Product $product ): bool { + /** + * The filter returning true if PayPal buttons/messages can be rendered for this product, or false otherwise. + */ + return apply_filters( + 'woocommerce_paypal_payments_product_supports_payment_request_button', + ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(), + $product + ); + } + /** * Retrieves all payment tokens for the user, via API or cached if already queried. *