From 4a81859bd40e79b58ab8e9f51ae30b60f503c067 Mon Sep 17 00:00:00 2001 From: Manish Menaria Date: Tue, 13 Aug 2024 14:57:51 +0530 Subject: [PATCH] Try a different logic --- .../Blocks/BlockTypes/ProductCollection.php | 49 +++++++++++++------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection.php index f0ff46bc275..1609b6091a4 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductCollection.php @@ -46,12 +46,16 @@ class ProductCollection extends AbstractBlock { */ protected $custom_order_opts = array( 'popularity', 'rating' ); + /** - * Whether the render of the block should be prevented. + * The render state of the product collection block. * - * @var bool + * @var array */ - private $prevent_render = null; + private $render_state = array( + 'has_results' => false, + 'has_no_results_block' => false, + ); /** * Initialize this block type. @@ -90,10 +94,7 @@ class ProductCollection extends AbstractBlock { add_filter( 'render_block_woocommerce/product-template', function ( $html ) { - if ( null === $this->prevent_render ) { - $this->prevent_render = '' === $html; - } - + $this->render_state['has_results'] = ! empty( $html ); return $html; }, 100, @@ -104,8 +105,7 @@ class ProductCollection extends AbstractBlock { add_filter( 'render_block_woocommerce/product-collection-no-results', function ( $html ) { - $this->prevent_render = false; - + $this->render_state['has_no_results_block'] = ! empty( $html ); return $html; }, 100, @@ -131,18 +131,37 @@ class ProductCollection extends AbstractBlock { * @return string */ public function handle_product_collection_rendering( $block_content, $block ) { - if ( true === $this->prevent_render ) { - $block_content = ''; - } else { - $block_content = $this->enhance_product_collection_with_interactivity( $block_content, $block ); + if ( $this->should_prevent_render() ) { + return ''; // Prevent rendering. } - // Reset the prevent_render flag. - $this->prevent_render = null; + // Reset the render state for the next render. + $this->reset_render_state(); return $block_content; } + /** + * Check if the block should be prevented from rendering. + * + * @return bool + */ + private function should_prevent_render() { + return ! $this->render_state['has_results'] && ! $this->render_state['has_no_results_block']; + } + + /** + * Reset the render state. + */ + private function reset_render_state() { + $this->render_state = array( + 'has_results' => false, + 'has_no_results_block' => false, + ); + } + + + /** * Provides the location context to each inner block of the product collection block. * Hint: Only blocks using the 'query' context will be affected.