From 5047182efd9fed9e674bfc7559f6de9bd07a8a7f Mon Sep 17 00:00:00 2001 From: Tung Du Date: Fri, 9 Aug 2024 16:37:45 +0700 Subject: [PATCH] Product Collection: Fix: ensure the global product object is always ready for compatibility layer (#49971) * add: method for block to declare extra block setting for registration * update: register Product Template block with skip_inner_blocks * chore: remove unused import * chore: changelog * Revert "update: register Product Template block with skip_inner_blocks" This reverts commit 88b076bde3b7fc6460b8d62e44669372ae385772. * Revert "add: method for block to declare extra block setting for registration" This reverts commit d1545dbba52bd050d7108f8ca2b78943e48c790f. * fix: filter block setting for product template * chore: changelog * Revert "chore: remove unused import" This reverts commit 0f8f66bad1f6090f266f2d4a4f7fccf2745427e7. --- ...ect-compatibility-layer-product-collection | 4 +++ .../src/Blocks/BlockTypes/ProductTemplate.php | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-global-product-object-compatibility-layer-product-collection diff --git a/plugins/woocommerce/changelog/fix-global-product-object-compatibility-layer-product-collection b/plugins/woocommerce/changelog/fix-global-product-object-compatibility-layer-product-collection new file mode 100644 index 00000000000..3e10c410ba2 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-global-product-object-compatibility-layer-product-collection @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix: ensure the global product object is always ready for compatibility layer by disabling default render routine of Product Templates inner blocks. diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductTemplate.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductTemplate.php index cafc22569f9..583f6a464c2 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductTemplate.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductTemplate.php @@ -17,6 +17,18 @@ class ProductTemplate extends AbstractBlock { */ protected $block_name = 'product-template'; + /** + * Initialize this block type. + * + * - Hook into WP lifecycle. + * - Register the block with WordPress. + * - Hook into pre_render_block to update the query. + */ + protected function initialize() { + add_filter( 'block_type_metadata_settings', array( $this, 'add_block_type_metadata_settings' ), 10, 2 ); + parent::initialize(); + } + /** * Get the frontend script handle for this block type. * @@ -134,4 +146,19 @@ class ProductTemplate extends AbstractBlock { return false; } + + /** + * Product Template renders inner blocks manually so we need to skip default + * rendering routine for its inner blocks + * + * @param array $settings Array of determined settings for registering a block type. + * @param array $metadata Metadata provided for registering a block type. + * @return array + */ + public function add_block_type_metadata_settings( $settings, $metadata ) { + if ( ! empty( $metadata['name'] ) && 'woocommerce/product-template' === $metadata['name'] ) { + $settings['skip_inner_blocks'] = true; + } + return $settings; + } }