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

Commit

Permalink
Set inherit default to true when products is inserted on archive prod…
Browse files Browse the repository at this point in the history
…ucts templates
  • Loading branch information
albarin committed Feb 2, 2023
1 parent 779409a commit 3f22d5e
Showing 1 changed file with 55 additions and 28 deletions.
83 changes: 55 additions & 28 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,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
Expand All @@ -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: (
<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;

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: (
<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' ],
} );
} );
}
}

0 comments on commit 3f22d5e

Please sign in to comment.