Commit dcd2d14ce3e for woocommerce
commit dcd2d14ce3e8b411b35f93f8ca4997afb5bcee59
Author: Albert Juhé Lluveras <contact@albertjuhe.com>
Date: Tue Mar 31 14:20:35 2026 +0200
Fix Add to Cart button not visible when displaying a Product block with a variation with only one product left (#63931)
* Fix Add to Cart button not visible when displaying a Single Product block with a variation with only one product left
* Add changelog
diff --git a/plugins/woocommerce/changelog/fix-WOO6-48-add-to-cart-variation-one-item b/plugins/woocommerce/changelog/fix-WOO6-48-add-to-cart-variation-one-item
new file mode 100644
index 00000000000..d4702470812
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-WOO6-48-add-to-cart-variation-one-item
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix Add to Cart button not visible when displaying a Single Product block with a variation with only one product left
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php
index 1f83ffe1a43..90ea3960bac 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/AddToCartWithOptions/AddToCartWithOptions.php
@@ -46,6 +46,18 @@ class AddToCartWithOptions extends AbstractBlock {
return apply_filters( '__experimental_woocommerce_' . $product_type . '_add_to_cart_with_options_block_template_part', false, $product_type );
}
+ /**
+ * Product type string used to resolve add-to-cart template parts.
+ * It returns the product type in most cases, except for variations,
+ * which use the simple product template.
+ *
+ * @param \WC_Product $product Product instance.
+ * @return string Product type slug.
+ */
+ private function get_product_type_for_add_to_cart_template( \WC_Product $product ): string {
+ return ProductType::VARIATION === $product->get_type() ? ProductType::SIMPLE : $product->get_type();
+ }
+
/**
* Enqueue assets specific to this block.
* We enqueue frontend scripts only if the product type has a block template
@@ -65,7 +77,8 @@ class AddToCartWithOptions extends AbstractBlock {
$rendered_product = wc_get_product( $product_id );
if ( $rendered_product instanceof \WC_Product ) {
- $template_part_path = $this->get_template_part_path( $rendered_product->get_type() );
+ $product_type = $this->get_product_type_for_add_to_cart_template( $rendered_product );
+ $template_part_path = $this->get_template_part_path( $product_type );
if ( is_string( $template_part_path ) && '' !== $template_part_path && file_exists( $template_part_path ) ) {
wp_enqueue_script_module( 'woocommerce/add-to-cart-with-options' );
@@ -177,8 +190,7 @@ class AddToCartWithOptions extends AbstractBlock {
return '';
}
- // For variations, we display the simple product form.
- $product_type = ProductType::VARIATION === $product->get_type() ? ProductType::SIMPLE : $product->get_type();
+ $product_type = $this->get_product_type_for_add_to_cart_template( $product );
$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array(), array( 'extra_classes' ) );
$classes = implode(