Commit 8c1df6c5a3d for woocommerce
commit 8c1df6c5a3d73ffcbe3dae79d19b1a88e180f0e2
Author: Albert Juhé Lluveras <contact@albertjuhe.com>
Date: Tue Oct 6 07:22:17 2026 +0200
Fix Featured Product block in classic themes (#69343)
* Fix Featured Product block in classic themes
* Add changelog
diff --git a/plugins/woocommerce/changelog/fix-featured-product-classic-themes b/plugins/woocommerce/changelog/fix-featured-product-classic-themes
new file mode 100644
index 00000000000..f1c7f8aab5e
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-featured-product-classic-themes
@@ -0,0 +1,5 @@
+Significance: patch
+Type: fix
+Comment: Fix fatal in Featured Product block when rendered in classic themes
+
+
diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/SingleProduct.php b/plugins/woocommerce/src/Blocks/BlockTypes/SingleProduct.php
index 0909bf263e0..c3665fd69dd 100644
--- a/plugins/woocommerce/src/Blocks/BlockTypes/SingleProduct.php
+++ b/plugins/woocommerce/src/Blocks/BlockTypes/SingleProduct.php
@@ -93,7 +93,6 @@ class SingleProduct extends AbstractBlock {
}
global $post;
- $this->previous_post = $post;
if ( empty( $context['postId'] ) ) {
return;
}
@@ -103,6 +102,13 @@ class SingleProduct extends AbstractBlock {
return;
}
+ // If $post already holds $product_post (this might happen in Featured Product),
+ // do nothing.
+ if ( $post instanceof \WP_Post && (int) $post->ID === (int) $product_post->ID ) {
+ return;
+ }
+
+ $this->previous_post = $post;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Core Post Title reads get_the_title() without a post ID.
$post = $product_post;
}
diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/FeaturedProductTest.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/FeaturedProductTest.php
index 30b15aa0f1b..5cb7226199a 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/FeaturedProductTest.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/FeaturedProductTest.php
@@ -80,6 +80,28 @@ class FeaturedProductTest extends WC_Unit_Test_Case {
);
}
+ /**
+ * @testdox Rendering a Featured Product title on a post leaves that post in place for the comments template.
+ */
+ public function test_featured_product_title_restores_enclosing_post(): void {
+ $this->product = WC_Helper_Product::create_simple_product( true, array( 'name' => 'Featured cap' ) );
+ $post_id = self::factory()->post->create( array( 'post_title' => 'Containing post' ) );
+ $this->go_to( get_permalink( $post_id ) );
+ $previous_post = $GLOBALS['post'];
+
+ $html = do_blocks(
+ sprintf(
+ '<!-- wp:woocommerce/featured-product {"productId":%d} -->
+ <!-- wp:post-title {"level":2} /-->
+ <!-- /wp:woocommerce/featured-product -->',
+ $this->product->get_id()
+ )
+ );
+
+ $this->assertStringContainsString( 'Featured cap', $html, 'The inner title should render the featured product title.' );
+ $this->assertSame( $previous_post, $GLOBALS['post'], 'The global post must be restored after the block renders.' );
+ }
+
/**
* @testdox Should not render a password-protected description in the legacy Featured Product block.
*/