Commit 0122b44265 for woocommerce
commit 0122b44265d6fb130cbea7e0958d9e0496eec755
Author: Amit Raj <77401999+amitraj2203@users.noreply.github.com>
Date: Fri Apr 25 17:29:11 2025 +0530
[Experimental] Blockified Add to Cart with Options block: Add utility method to check if a simple product is not purchasable (#57523)
* refactor: AddToCartWithOptions logic to use utility method for checking non-purchasable simple products
* feat: Rename AddtoCartWithOptions folder name
* feat: Rename the files where Utils class of AddToCartWithOptions is being used.
* feat: Add changelog file
diff --git a/plugins/woocommerce/changelog/57523-add-not-purchasable-simple-product-utility b/plugins/woocommerce/changelog/57523-add-not-purchasable-simple-product-utility
new file mode 100644
index 0000000000..82369401e6
--- /dev/null
+++ b/plugins/woocommerce/changelog/57523-add-not-purchasable-simple-product-utility
@@ -0,0 +1,5 @@
+Significance: patch
+Type: enhancement
+Comment: Add utility method to check if a simple product is not purchasable or not in stock in AddToCartWithOptions Block.
+
+
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddtoCartWithOptions/Utils.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/Utils.php
similarity index 90%
rename from plugins/woocommerce/src/Blocks/BlockTypes/AddtoCartWithOptions/Utils.php
rename to plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/Utils.php
index a28c8bd288..757f464305 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddtoCartWithOptions/Utils.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/Utils.php
@@ -1,7 +1,9 @@
<?php
declare(strict_types=1);
-namespace Automattic\WooCommerce\Blocks\BlockTypes\AddtoCartWithOptions;
+namespace Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions;
+
+use Automattic\WooCommerce\Enums\ProductType;
/**
* Utility methods used for the Add to Cart with Options block.
@@ -120,4 +122,14 @@ class Utils {
return $product instanceof \WC_Product ? $product : null;
}
+
+ /**
+ * Check if a product is a simple product that is not purchasable or not in stock.
+ *
+ * @param \WC_Product $product The product to check.
+ * @return bool True if the product is a simple product that is not purchasable or not in stock.
+ */
+ public static function is_not_purchasable_simple_product( $product ) {
+ return ProductType::SIMPLE === $product->get_type() && ( ! $product->is_in_stock() || ! $product->is_purchasable() );
+ }
}
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsGroupedProductSelectorItemCTA.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsGroupedProductSelectorItemCTA.php
index 9b4b3cdbaa..17dc9a9e74 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsGroupedProductSelectorItemCTA.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsGroupedProductSelectorItemCTA.php
@@ -3,7 +3,7 @@ declare(strict_types=1);
namespace Automattic\WooCommerce\Blocks\BlockTypes;
-use Automattic\WooCommerce\Blocks\BlockTypes\AddtoCartWithOptions\Utils;
+use Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions\Utils;
use WP_Block;
/**
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsQuantitySelector.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsQuantitySelector.php
index ae7c8889d3..db79df911b 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsQuantitySelector.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptionsQuantitySelector.php
@@ -3,9 +3,8 @@ declare(strict_types=1);
namespace Automattic\WooCommerce\Blocks\BlockTypes;
-use Automattic\WooCommerce\Blocks\BlockTypes\AddtoCartWithOptions\Utils;
+use Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions\Utils;
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;
-use Automattic\WooCommerce\Enums\ProductType;
/**
* AddToCartWithOptionsQuantitySelector class.
@@ -46,7 +45,7 @@ class AddToCartWithOptionsQuantitySelector extends AbstractBlock {
return '';
}
- if ( ProductType::SIMPLE === $product->get_type() && ( ! $product->is_in_stock() || ! $product->is_purchasable() ) ) {
+ if ( Utils::is_not_purchasable_simple_product( $product ) ) {
$product = $previous_product;
return '';
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
index 06744e6c94..e9c82e7caf 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductButton.php
@@ -5,7 +5,7 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes;
use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils;
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;
-use Automattic\WooCommerce\Enums\ProductType;
+use Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions\Utils;
/**
* ProductButton class.
@@ -88,7 +88,7 @@ class ProductButton extends AbstractBlock {
$is_descendent_of_add_to_cart_form = isset( $block->context['woocommerce/isDescendantOfAddToCartWithOptions'] ) ? $block->context['woocommerce/isDescendantOfAddToCartWithOptions'] : false;
- if ( $is_descendent_of_add_to_cart_form && ProductType::SIMPLE === $product->get_type() && ( ! $product->is_in_stock() || ! $product->is_purchasable() ) ) {
+ if ( $is_descendent_of_add_to_cart_form && Utils::is_not_purchasable_simple_product( $product ) ) {
$product = $previous_product;
return '';