From 3f22d5eefaaa3b345b44ac84a1ff462861f8f371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Thu, 2 Feb 2023 14:27:09 +0100 Subject: [PATCH] Set inherit default to true when products is inserted on archive products templates --- .../variations/product-query.tsx | 83 ++++++++++++------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/assets/js/blocks/product-query/variations/product-query.tsx b/assets/js/blocks/product-query/variations/product-query.tsx index 73362b8bf0d..cbb923de953 100644 --- a/assets/js/blocks/product-query/variations/product-query.tsx +++ b/assets/js/blocks/product-query/variations/product-query.tsx @@ -6,6 +6,8 @@ import { Icon } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { stacks } from '@woocommerce/icons'; import { isWpVersion } from '@woocommerce/settings'; +import { isExperimentalBuild } from '@woocommerce/block-settings'; +import { select, subscribe } from '@wordpress/data'; /** * Internal dependencies @@ -19,33 +21,58 @@ import { const VARIATION_NAME = 'woocommerce/product-query'; +const ARCHIVE_PRODUCT_TEMPLATES = [ + 'woocommerce/woocommerce//archive-product', + 'woocommerce/woocommerce//taxonomy-product_cat', + 'woocommerce/woocommerce//taxonomy-product_tag', + 'woocommerce/woocommerce//taxonomy-product_attribute', + 'woocommerce/woocommerce//product-search-results', +]; + if ( isWpVersion( '6.1', '>=' ) ) { - registerBlockVariation( QUERY_LOOP_ID, { - description: __( - 'A block that displays a selection of products in your store.', - 'woo-gutenberg-products-block' - ), - name: VARIATION_NAME, - /* translators: “Products“ is the name of the block. */ - title: __( 'Products (Beta)', 'woo-gutenberg-products-block' ), - isActive: ( blockAttributes ) => - blockAttributes.namespace === VARIATION_NAME, - icon: ( - - ), - attributes: { - ...QUERY_DEFAULT_ATTRIBUTES, - namespace: VARIATION_NAME, - }, - // Gutenberg doesn't support this type yet, discussion here: - // https://github.com/WordPress/gutenberg/pull/43632 - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - allowedControls: DEFAULT_ALLOWED_CONTROLS, - innerBlocks: INNER_BLOCKS_TEMPLATE, - scope: [ 'inserter' ], - } ); + let currentTemplateId: string | undefined; + + if ( isExperimentalBuild() ) { + subscribe( () => { + const previousTemplateId = currentTemplateId; + const store = select( 'core/edit-site' ); + currentTemplateId = store?.getEditedPostId(); + + if ( previousTemplateId === currentTemplateId ) { + return; + } + + QUERY_DEFAULT_ATTRIBUTES.query.inherit = + ARCHIVE_PRODUCT_TEMPLATES.includes( currentTemplateId ); + + registerBlockVariation( QUERY_LOOP_ID, { + description: __( + 'A block that displays a selection of products in your store.', + 'woo-gutenberg-products-block' + ), + name: VARIATION_NAME, + /* translators: “Products“ is the name of the block. */ + title: __( 'Products (Beta)', 'woo-gutenberg-products-block' ), + isActive: ( blockAttributes ) => + blockAttributes.namespace === VARIATION_NAME, + icon: ( + + ), + attributes: { + ...QUERY_DEFAULT_ATTRIBUTES, + namespace: VARIATION_NAME, + }, + // Gutenberg doesn't support this type yet, discussion here: + // https://github.com/WordPress/gutenberg/pull/43632 + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + allowedControls: DEFAULT_ALLOWED_CONTROLS, + innerBlocks: INNER_BLOCKS_TEMPLATE, + scope: [ 'inserter' ], + } ); + } ); + } }