Commit 3181fb3734c for woocommerce
commit 3181fb3734c3ea6015dc8abebdf3b2e1bb202405
Author: Anuj Singh <80690679+Anuj-Rathore24@users.noreply.github.com>
Date: Wed Jun 24 16:31:22 2026 +0530
Fix: WC_Blocks_Utils::has_block_in_page() fails to detect blocks nested inside container blocks (#63386)
* Fix: make WC_Blocks_Utils::has_block_in_page() search nested blocks recursively
* feat: add tests for WC_Blocks_Utils::has_block_in_page() nested block detection
* feat: add changelog entry WC_Blocks_Utils::has_block_in_page() search nested blocks fix
* refactor: Resolve linting warnings.
* refactor: add strict_types
* fix: use core has_block() in has_block_in_page() for nested block detection
* refactor: Remove strict type declaration
diff --git a/plugins/woocommerce/changelog/fix-has-block-in-page-recursive-search b/plugins/woocommerce/changelog/fix-has-block-in-page-recursive-search
new file mode 100644
index 00000000000..684623246fb
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-has-block-in-page-recursive-search
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix - Make WC_Blocks_Utils::has_block_in_page() search nested blocks recursively.
diff --git a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php
index 4ecb4c212e5..4ff017aabf4 100644
--- a/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php
+++ b/plugins/woocommerce/includes/blocks/class-wc-blocks-utils.php
@@ -60,25 +60,15 @@ class WC_Blocks_Utils {
}
/**
- * Check if a given page contains a particular block.
+ * Determine if a page has a block in its content.
*
- * @param int|WP_Post $page Page post ID or post object.
+ * @param int|WP_Post $page Page post ID or post object.
* @param string $block_name The name (id) of a block, e.g. `woocommerce/cart`.
- * @return bool Boolean value if the page contains the block or not. Null in case the page does not exist.
+ * @return bool True if the page contains the block, false otherwise.
+ *
+ * @see has_block()
*/
- public static function has_block_in_page( $page, $block_name ) {
- $page_to_check = get_post( $page );
- if ( null === $page_to_check ) {
- return false;
- }
-
- $blocks = parse_blocks( $page_to_check->post_content );
- foreach ( $blocks as $block ) {
- if ( $block_name === $block['blockName'] ) {
- return true;
- }
- }
-
- return false;
+ public static function has_block_in_page( $page, $block_name ): bool {
+ return has_block( $block_name, $page );
}
}
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/blocks/class-wc-tests-blocks-utils.php b/plugins/woocommerce/tests/legacy/unit-tests/blocks/class-wc-tests-blocks-utils.php
index 907846f88f2..5e7d8efbc13 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/blocks/class-wc-tests-blocks-utils.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/blocks/class-wc-tests-blocks-utils.php
@@ -71,6 +71,51 @@ class WC_Test_Blocks_Utils extends WC_Unit_Test_Case {
$this->assertTrue( WC_Blocks_Utils::has_block_in_page( $page_id, 'core/heading' ) );
}
+ /**
+ * @group block-utils
+ * Test: has_block_in_page detects block nested multiple levels deep.
+ */
+ public function test_has_block_in_page_detects_block_nested_multiple_levels_deep() {
+ $page = array(
+ 'name' => 'checkout-page-deep',
+ 'title' => 'Checkout Deep',
+ 'content' => '<!-- wp:group -->
+ <!-- wp:columns -->
+ <!-- wp:column -->
+ <!-- wp:woocommerce/checkout -->
+ <!-- /wp:woocommerce/checkout -->
+ <!-- /wp:column -->
+ <!-- /wp:columns -->
+ <!-- /wp:group -->',
+ );
+
+ $page_id = wc_create_page( $page['name'], '', $page['title'], $page['content'] );
+
+ $this->assertTrue( WC_Blocks_Utils::has_block_in_page( $page_id, 'woocommerce/checkout' ) );
+ }
+
+ /**
+ * @group block-utils
+ * Test: has_block_in_page returns false when nested block is not present.
+ */
+ public function test_has_block_in_page_returns_false_when_nested_block_not_present() {
+ $page = array(
+ 'name' => 'checkout-page-no-block',
+ 'title' => 'Checkout No Block',
+ 'content' => '<!-- wp:columns -->
+ <!-- wp:column -->
+ <!-- wp:paragraph -->
+ <p>Some text</p>
+ <!-- /wp:paragraph -->
+ <!-- /wp:column -->
+ <!-- /wp:columns -->',
+ );
+
+ $page_id = wc_create_page( $page['name'], '', $page['title'], $page['content'] );
+
+ $this->assertFalse( WC_Blocks_Utils::has_block_in_page( $page_id, 'woocommerce/checkout' ) );
+ }
+
/**
* @group block-utils
* Test: get_all_blocks_from_page.
@@ -87,19 +132,19 @@ class WC_Test_Blocks_Utils extends WC_Unit_Test_Case {
$expected = array(
0 => array(
- 'blockName' => 'core/heading',
- 'attrs' => array(),
- 'innerBlocks' => array(),
- 'innerHTML' => '<h2>test1</h2>',
+ 'blockName' => 'core/heading',
+ 'attrs' => array(),
+ 'innerBlocks' => array(),
+ 'innerHTML' => '<h2>test1</h2>',
'innerContent' => array(
0 => '<h2>test1</h2>',
),
),
1 => array(
- 'blockName' => 'core/heading',
- 'attrs' => array(),
- 'innerBlocks' => array(),
- 'innerHTML' => '<h1>test2</h1>',
+ 'blockName' => 'core/heading',
+ 'attrs' => array(),
+ 'innerBlocks' => array(),
+ 'innerHTML' => '<h1>test2</h1>',
'innerContent' => array(
0 => '<h1>test2</h1>',
),