Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Set inherit default to true when products is inserted on archive product templates #8375

Merged
merged 13 commits into from
Feb 13, 2023
78 changes: 51 additions & 27 deletions assets/js/blocks/product-query/variations/product-query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Icon } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { stacks } from '@woocommerce/icons';
import { isWpVersion } from '@woocommerce/settings';
import { select, subscribe } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -19,33 +20,56 @@ 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: (
<Icon
icon={ stacks }
className="wc-block-editor-components-block-icon wc-block-editor-components-block-icon--stacks"
/>
),
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;

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 );
albarin marked this conversation as resolved.
Show resolved Hide resolved

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: (
<Icon
icon={ stacks }
className="wc-block-editor-components-block-icon wc-block-editor-components-block-icon--stacks"
/>
),
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' ],
} );
albarin marked this conversation as resolved.
Show resolved Hide resolved
} );
}