From ebda39d1309c8a4250d25fb79ae1b45ac1696ff1 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Wed, 1 Jun 2022 18:17:51 +0400 Subject: [PATCH] If product has inStock variation then allow buttons --- .../ppcp-button/src/Assets/SmartButton.php | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index f15b8c800..0391ea5b8 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Button\Assets; use Exception; use Psr\Log\LoggerInterface; use WC_Product; +use WC_Product_Variation; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; @@ -1252,9 +1253,25 @@ class SmartButton implements SmartButtonInterface { /** * The filter returning true if PayPal buttons/messages can be rendered for this product, or false otherwise. */ + + $in_stock = $product->is_in_stock(); + + if ( $product->is_type( 'variable' ) ) { + /** + * The method is defined in WC_Product_Variable class. + * + * @psalm-suppress UndefinedMethod + */ + $variations = $product->get_available_variations( 'objects' ); + $in_stock = $this->has_in_stock_variation( $variations ); + } + + /** + * Allows to filter if PayPal buttons/messages can be rendered for the given product. + */ return apply_filters( 'woocommerce_paypal_payments_product_supports_payment_request_button', - ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(), + ! $product->is_type( array( 'external', 'grouped' ) ) && $in_stock, $product ); } @@ -1291,4 +1308,20 @@ class SmartButton implements SmartButtonInterface { } return ''; } + + /** + * Checks if variations contain any in stock variation. + * + * @param WC_Product_Variation[] $variations The list of variations. + * @return bool True if any in stock variation, false otherwise. + */ + protected function has_in_stock_variation( array $variations ): bool { + foreach ( $variations as $variation ) { + if ( $variation->is_in_stock() ) { + return true; + } + } + + return false; + } }