Commit 440453f3fcc for woocommerce
commit 440453f3fccb6acb3c5e294829e5b5b6140230ff
Author: SH Sajal Chowdhury <72102985+shsajalchowdhury@users.noreply.github.com>
Date: Fri May 1 22:10:23 2026 +0600
Fix PHP warning for undefined array key fixedWidth in ProductCollection Renderer (#64432)
* Fix PHP warning for undefined array key fixedWidth in ProductCollection Renderer
When the Product Collection block is configured with Fixed Width + Carousel,
the fixedWidth key may be missing from block attributes (the TypeScript type
marks it as optional). Accessing it without checking caused a PHP warning.
Added a !empty() guard so the fixed width style is only applied when the
fixedWidth value is actually present and non-empty.
Fixes #63918
* chore: fix lint errors in RendererDimensions test file
* chore: refresh PHPStan baseline after merging trunk
---------
Co-authored-by: Brandon Kraft <public@brandonkraft.com>
diff --git a/plugins/woocommerce/changelog/fix-63918-product-collection-fixedwidth-undefined b/plugins/woocommerce/changelog/fix-63918-product-collection-fixedwidth-undefined
new file mode 100644
index 00000000000..d84a5ce2a20
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-63918-product-collection-fixedwidth-undefined
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix PHP warning for undefined array key fixedWidth in ProductCollection Renderer when using Fixed Width + Carousel
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Renderer.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Renderer.php
index 536618087c2..2b8a1f2c3aa 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Renderer.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection/Renderer.php
@@ -257,7 +257,7 @@ class Renderer {
*/
private function handle_block_dimensions( $p, $block ) {
if ( isset( $block['attrs']['dimensions'] ) && isset( $block['attrs']['dimensions']['widthType'] ) ) {
- if ( 'fixed' === $block['attrs']['dimensions']['widthType'] ) {
+ if ( 'fixed' === $block['attrs']['dimensions']['widthType'] && ! empty( $block['attrs']['dimensions']['fixedWidth'] ) ) {
$this->set_fixed_width_style( $p, $block['attrs']['dimensions']['fixedWidth'] );
}
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection/RendererDimensions.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection/RendererDimensions.php
new file mode 100644
index 00000000000..7eac142e4b8
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection/RendererDimensions.php
@@ -0,0 +1,128 @@
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes\ProductCollection;
+
+use Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection\Renderer;
+use WC_Unit_Test_Case;
+
+/**
+ * Tests for the ProductCollection Renderer dimension handling.
+ */
+class RendererDimensions extends WC_Unit_Test_Case {
+
+ /**
+ * Test that handle_block_dimensions does not throw a warning when fixedWidth is missing.
+ *
+ * @testdox No PHP warning when widthType is 'fixed' but fixedWidth key is absent from block attributes.
+ */
+ public function test_no_warning_when_fixedwidth_is_missing(): void {
+ $renderer = new Renderer();
+ $html = '<div class="wc-block-product-collection"><ul><li>Product</li></ul></div>';
+ $p = new \WP_HTML_Tag_Processor( $html );
+
+ $p->next_tag( 'div' );
+
+ $block = array(
+ 'attrs' => array(
+ 'dimensions' => array(
+ 'widthType' => 'fixed',
+ ),
+ ),
+ );
+
+ // Should not throw a PHP warning when fixedWidth is missing.
+ $method = new \ReflectionMethod( $renderer, 'handle_block_dimensions' );
+ $method->setAccessible( true );
+ $method->invoke( $renderer, $p, $block );
+
+ // The div should not have a style attribute set since fixedWidth was not provided.
+ $this->assertNull( $p->get_attribute( 'style' ), 'Style attribute should not be set when fixedWidth is missing.' );
+ }
+
+ /**
+ * Test that handle_block_dimensions applies style when fixedWidth is provided.
+ *
+ * @testdox Style is applied when widthType is 'fixed' and fixedWidth has a valid value.
+ */
+ public function test_style_applied_when_fixedwidth_is_provided(): void {
+ $renderer = new Renderer();
+ $html = '<div class="wc-block-product-collection"><ul><li>Product</li></ul></div>';
+ $p = new \WP_HTML_Tag_Processor( $html );
+
+ $p->next_tag( 'div' );
+
+ $block = array(
+ 'attrs' => array(
+ 'dimensions' => array(
+ 'widthType' => 'fixed',
+ 'fixedWidth' => '300px',
+ ),
+ ),
+ );
+
+ $method = new \ReflectionMethod( $renderer, 'handle_block_dimensions' );
+ $method->setAccessible( true );
+ $method->invoke( $renderer, $p, $block );
+
+ $style = $p->get_attribute( 'style' );
+ $this->assertNotNull( $style, 'Style attribute should be set when fixedWidth is provided.' );
+ $this->assertStringContainsString( '300px', $style, 'Style should contain the fixed width value.' );
+ }
+
+ /**
+ * Test that handle_block_dimensions does not apply style when fixedWidth is empty string.
+ *
+ * @testdox No style applied when fixedWidth is an empty string.
+ */
+ public function test_no_style_when_fixedwidth_is_empty_string(): void {
+ $renderer = new Renderer();
+ $html = '<div class="wc-block-product-collection"><ul><li>Product</li></ul></div>';
+ $p = new \WP_HTML_Tag_Processor( $html );
+
+ $p->next_tag( 'div' );
+
+ $block = array(
+ 'attrs' => array(
+ 'dimensions' => array(
+ 'widthType' => 'fixed',
+ 'fixedWidth' => '',
+ ),
+ ),
+ );
+
+ $method = new \ReflectionMethod( $renderer, 'handle_block_dimensions' );
+ $method->setAccessible( true );
+ $method->invoke( $renderer, $p, $block );
+
+ $this->assertNull( $p->get_attribute( 'style' ), 'Style attribute should not be set when fixedWidth is empty.' );
+ }
+
+ /**
+ * Test that handle_block_dimensions does nothing when widthType is 'fill'.
+ *
+ * @testdox No style applied when widthType is not 'fixed'.
+ */
+ public function test_no_style_when_widthtype_is_fill(): void {
+ $renderer = new Renderer();
+ $html = '<div class="wc-block-product-collection"><ul><li>Product</li></ul></div>';
+ $p = new \WP_HTML_Tag_Processor( $html );
+
+ $p->next_tag( 'div' );
+
+ $block = array(
+ 'attrs' => array(
+ 'dimensions' => array(
+ 'widthType' => 'fill',
+ 'fixedWidth' => '300px',
+ ),
+ ),
+ );
+
+ $method = new \ReflectionMethod( $renderer, 'handle_block_dimensions' );
+ $method->setAccessible( true );
+ $method->invoke( $renderer, $p, $block );
+
+ $this->assertNull( $p->get_attribute( 'style' ), 'Style attribute should not be set when widthType is fill.' );
+ }
+}