From 75ebaa92de17ff8c5832a234cecded31df72dcc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Fri, 20 Jan 2023 15:24:18 +0100 Subject: [PATCH] Set inherit default to true only when is inserted in the archive product template --- assets/js/blocks/product-query/constants.ts | 7 ++++++- src/BlockTypes/ProductQuery.php | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/assets/js/blocks/product-query/constants.ts b/assets/js/blocks/product-query/constants.ts index 39c84b8539d..ff844f701f8 100644 --- a/assets/js/blocks/product-query/constants.ts +++ b/assets/js/blocks/product-query/constants.ts @@ -47,6 +47,11 @@ const GLOBAL_HIDE_OUT_OF_STOCK = getSetting< boolean >( false ); +const isArchiveProductTemplate = getSetting< boolean >( + 'is_archive_product_template', + false +); + export const QUERY_DEFAULT_ATTRIBUTES: QueryBlockAttributes = { allowedControls: DEFAULT_ALLOWED_CONTROLS, displayLayout: { @@ -64,7 +69,7 @@ export const QUERY_DEFAULT_ATTRIBUTES: QueryBlockAttributes = { search: '', exclude: [], sticky: '', - inherit: false, + inherit: isArchiveProductTemplate, __woocommerceAttributes: [], __woocommerceStockStatus: GLOBAL_HIDE_OUT_OF_STOCK ? Object.keys( objectOmit( STOCK_STATUS_OPTIONS, 'outofstock' ) ) diff --git a/src/BlockTypes/ProductQuery.php b/src/BlockTypes/ProductQuery.php index 6b63c16d489..b1c694ad41f 100644 --- a/src/BlockTypes/ProductQuery.php +++ b/src/BlockTypes/ProductQuery.php @@ -113,6 +113,23 @@ public function update_query( $pre_render, $parsed_block ) { } } + /** + * Extra data passed through from server to client for block. + * + * @param array $attributes Any attributes that currently are available from the block. + * Note, this will be empty in the editor context when the block is + * not in the post content on editor load. + */ + protected function enqueue_data( array $attributes = [] ) { + parent::enqueue_data( $attributes ); + + global $pagenow; + // phpcs:ignore WordPress.Security.NonceVerification.Recommended + $post_id = isset( $_GET['postId'] ) ? sanitize_text_field( wp_unslash( $_GET['postId'] ) ) : null; + $is_archive_product_template = 'site-editor.php' === $pagenow && 'woocommerce/woocommerce//archive-product' === $post_id; + $this->asset_data_registry->add( 'is_archive_product_template', $is_archive_product_template, true ); + } + /** * Update the query for the product query block in Editor. *