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

Product Collection - Add Inherit query from template control #9485

Merged
merged 74 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
e4abb25
Add columns control to product collection block editor settings
imanish003 May 15, 2023
43b2d1c
Merge branch 'trunk' into 9356-product-collection-editor-settings-col…
imanish003 May 15, 2023
65582af
Refactor: Simplify Fallback Return in ColumnsControl Component
imanish003 May 16, 2023
e03ecf6
Feature: Add 'Order By' Control to Product Collection Inspector
imanish003 May 16, 2023
91b5cb4
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
imanish003 May 16, 2023
5e5c6b1
Add more options to OrderBy type
imanish003 May 16, 2023
502295f
Add Inherit global query control to the Product Collection
kmanijak May 16, 2023
9cfa07e
Merge branch 'trunk' into 9359-product-collection-editor-settings-ord…
imanish003 May 17, 2023
7954c13
Add orderby handling on frontend & editor
imanish003 May 17, 2023
b756198
Merge branch 'trunk' into 9359-product-collection-editor-settings-ord…
imanish003 May 17, 2023
bed6be4
Merge branch 'trunk' into 9359-product-collection-editor-settings-ord…
imanish003 May 18, 2023
58c518a
Merge branch 'trunk' into add/product-collection-inherit
kmanijak May 18, 2023
6978335
Fix the condition to show query controls (they shoul appear if query …
kmanijak May 18, 2023
df9fad6
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 18, 2023
d257e8b
Make Product Collection inheirt global query in product archive templ…
kmanijak May 18, 2023
724f894
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
imanish003 May 19, 2023
e5a43cf
Add 'on sale' filter and enhance settings management in product colle…
imanish003 May 19, 2023
2e77956
Merge branch 'trunk' into 9361-product-collection-filters-on-sale
imanish003 May 22, 2023
8e9ee85
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 22, 2023
1022323
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 23, 2023
229d12b
Revert "Make Product Collection inheirt global query in product archi…
imanish003 May 23, 2023
1828047
Updated 'inherit' behavior in the Product Collection block
imanish003 May 23, 2023
c2d59c1
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 24, 2023
a832d97
Add stock status filter to WooCommerce product collection block
imanish003 May 24, 2023
c4188da
Refactor Stock Status control of Product Collection block
imanish003 May 24, 2023
2d5e3f9
Merge branch 'trunk' into 9362-product-collection-filters-stock-status
imanish003 May 24, 2023
07fc6fe
Add keyword search control to Product Collection block
imanish003 May 24, 2023
1f6a425
Resolve conflicts
imanish003 May 25, 2023
751b2b4
Add product attributes filter control to ProductCollection block
imanish003 May 25, 2023
ede31c9
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 25, 2023
c71c4c5
Merge branch 'trunk' into 9363-product-collection-filters-attributes
thealexandrelara May 25, 2023
c000167
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 26, 2023
acbdc06
Resolve conflicts
imanish003 May 26, 2023
3b1b6c6
remove unused import of getDefaultStockStatuses
imanish003 May 26, 2023
287b91f
Resolve conflicts
imanish003 May 26, 2023
67446de
Delete a duplicate file
imanish003 May 26, 2023
fa957fd
Resolve conflicts
imanish003 May 26, 2023
cacd280
Remove console log
imanish003 May 29, 2023
afa0e46
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 29, 2023
aef2878
Add taxonomies control to Product collection block
imanish003 May 29, 2023
24031a6
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 May 29, 2023
f9a6813
Address PR feedback & other improvements
imanish003 May 30, 2023
7218533
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 30, 2023
7cbd3a8
Merge branch '9363-product-collection-filters-attributes' of https://…
imanish003 May 30, 2023
e049cf8
Address PR review comments
imanish003 May 30, 2023
3a8dc95
Add wc-block-editor prefix to className
imanish003 May 30, 2023
edf824b
Merge branch 'trunk' into 9363-product-collection-filters-attributes
imanish003 May 30, 2023
20fdaf9
Merge branch '9363-product-collection-filters-attributes' of https://…
imanish003 May 30, 2023
9e08ade
Resolve conflicts
imanish003 May 30, 2023
c10388e
Make improvements to 'inherit' functionality in Product collection bl…
imanish003 May 30, 2023
c5b92fc
Resolve conflicts
imanish003 May 30, 2023
0fecc33
Improve product collection query inheritance and fix URL typo
imanish003 May 30, 2023
5f60aa1
Resolve conflicts
imanish003 May 30, 2023
5528cf6
Minor improvemnets
imanish003 May 30, 2023
49f4fef
Merge branch 'trunk' into add/product-collection-inherit
imanish003 May 30, 2023
872b637
Resolve conflicts
imanish003 May 30, 2023
90d133a
Add wc-block-editor- prefix with classNames
imanish003 May 30, 2023
5f1a5bb
Handle duplicate taxonomy names in Taxonomy controls
imanish003 May 31, 2023
ba0ce51
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 May 31, 2023
d1cfe70
Remove isset() in if condition as it's unnecessary
imanish003 May 31, 2023
dbe02a9
Refactor TaxonomyItem component for better readability
imanish003 May 31, 2023
1be8967
Handling for duplicate term names & other improvements
imanish003 May 31, 2023
734d251
Restructure taxonomy controls in product collection block
imanish003 Jun 1, 2023
414d379
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 Jun 1, 2023
5e6d9d9
Merge branch '9364-product-collection-filters-taxonomies' of https://…
imanish003 Jun 1, 2023
6a10f3f
Fix case insensitive search support for FormTokenField
imanish003 Jun 2, 2023
d9d7cd7
Refactor getTermIdByTermValue function and update newSuggestions mapping
imanish003 Jun 2, 2023
7f72924
Merge branch 'trunk' into 9364-product-collection-filters-taxonomies
imanish003 Jun 2, 2023
7b56958
Optimize term fetching and initial search state in TaxonomyItem
imanish003 Jun 2, 2023
028aa9b
Merge branch '9364-product-collection-filters-taxonomies' into add/pr…
imanish003 Jun 2, 2023
af2de33
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
imanish003 Jun 2, 2023
34762c5
Address PR comments and other improvements
imanish003 Jun 2, 2023
ef6b436
Add with types import statement
imanish003 Jun 5, 2023
b13f560
Merge branch 'trunk' into add/product-collection-inherit
imanish003 Jun 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/js/base/hooks/use-previous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Validation< T > {
( value: T, previousValue: T | undefined ): boolean;
}
/**
* Use Previous based on https://usehooks.com/usePrevious/.
* Use Previous based on https://usehooks.com/useprevious/.
*
* @param {*} value
* @param {Function} [validation] Function that needs to validate for the value
Expand Down
3 changes: 2 additions & 1 deletion assets/js/blocks/product-collection/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const DEFAULT_QUERY: ProductCollectionQuery = {
search: '',
exclude: [],
sticky: '',
inherit: false,
inherit: null,
taxQuery: {},
parents: [],
isProductCollectionBlock: true,
Expand All @@ -69,6 +69,7 @@ export const getDefaultSettings = (
...currentAttributes.query,
orderBy: DEFAULT_QUERY.orderBy as TProductCollectionOrderBy,
order: DEFAULT_QUERY.order as TProductCollectionOrder,
inherit: DEFAULT_QUERY.inherit,
},
} );

Expand Down
39 changes: 30 additions & 9 deletions assets/js/blocks/product-collection/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import { useEffect } from '@wordpress/element';
* Internal dependencies
*/
import { ImageSizing } from '../../atomic/blocks/product-elements/image/types';
import { ProductCollectionAttributes } from './types';
import type {
ProductCollectionAttributes,
ProductCollectionQuery,
} from './types';
import { VARIATION_NAME as PRODUCT_TITLE_ID } from './variations/elements/product-title';
import InspectorControls from './inspector-controls';
import { DEFAULT_ATTRIBUTES } from './constants';
import './editor.scss';
import { getDefaultValueOfInheritQueryFromTemplate } from './utils';

export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
[
Expand Down Expand Up @@ -84,14 +88,6 @@ const Edit = ( props: BlockEditProps< ProductCollectionAttributes > ) => {

const instanceId = useInstanceId( Edit );

/**
* Because of issue https://github.com/WordPress/gutenberg/issues/7342,
* We are using this workaround to set default attributes.
*/
useEffect( () => {
setAttributes( { ...DEFAULT_ATTRIBUTES, ...attributes } );
}, [ setAttributes ] );

// We need this for multi-query block pagination.
// Query parameters for each block are scoped to their ID.
useEffect( () => {
Expand All @@ -100,6 +96,31 @@ const Edit = ( props: BlockEditProps< ProductCollectionAttributes > ) => {
}
}, [ queryId, instanceId, setAttributes ] );

/**
* Because of issue https://github.com/WordPress/gutenberg/issues/7342,
* We are using this workaround to set default attributes.
*/
useEffect( () => {
setAttributes( {
...DEFAULT_ATTRIBUTES,
query: {
...( DEFAULT_ATTRIBUTES.query as ProductCollectionQuery ),
inherit: getDefaultValueOfInheritQueryFromTemplate(),
},
...( attributes as Partial< ProductCollectionAttributes > ),
} );
// We don't wanna add attributes as a dependency here.
// Because we want this to run only once.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ setAttributes ] );

/**
* If inherit is not a boolean, then we haven't set default attributes yet.
* We don't wanna render anything until default attributes are set.
* Default attributes are set in the useEffect above.
*/
if ( typeof attributes?.query?.inherit !== 'boolean' ) return null;

return (
<div { ...blockProps }>
<InspectorControls { ...props } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const ColumnsControl = (
} }
>
<RangeControl
label={ __( 'Columns', 'woo-gutenberg-products-block' ) }
value={ columns }
onChange={ ( value: number ) =>
props.setAttributes( {
Expand Down
62 changes: 37 additions & 25 deletions assets/js/blocks/product-collection/inspector-controls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { BlockEditProps } from '@wordpress/blocks';
import { InspectorControls } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { AttributeMetadata } from '@woocommerce/types';
import {
// @ts-expect-error Using experimental features
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
Expand All @@ -17,6 +16,7 @@ import {
*/
import { ProductCollectionAttributes } from '../types';
import ColumnsControl from './columns-control';
import InheritQueryControl from './inherit-query-control';
import OrderByControl from './order-by-control';
import OnSaleControl from './on-sale-control';
import { setQueryAttribute } from '../utils';
Expand All @@ -29,6 +29,10 @@ import TaxonomyControls from './taxonomy-controls';
const ProductCollectionInspectorControls = (
props: BlockEditProps< ProductCollectionAttributes >
) => {
const query = props.attributes.query;
const inherit = query?.inherit;
const displayQueryControls = inherit === false;

const setQueryAttributeBind = useMemo(
() => setQueryAttribute.bind( null, props ),
[ props ]
Expand All @@ -46,33 +50,41 @@ const ProductCollectionInspectorControls = (
} }
>
<ColumnsControl { ...props } />
<OrderByControl { ...props } />
</ToolsPanel>

<ToolsPanel
label={ __( 'Filters', 'woo-gutenberg-products-block' ) }
resetAll={ ( resetAllFilters: ( () => void )[] ) => {
setQueryAttribute( props, DEFAULT_FILTERS );
resetAllFilters.forEach( ( resetFilter ) => resetFilter() );
} }
className="wc-block-editor-product-collection-inspector-toolspanel__filters"
>
<OnSaleControl { ...props } />
<StockStatusControl { ...props } />
<KeywordControl { ...props } />
<AttributesControl
woocommerceAttributes={
props.attributes.query &&
( props.attributes.query
.woocommerceAttributes as AttributeMetadata[] )
}
setQueryAttribute={ setQueryAttributeBind }
/>
<TaxonomyControls
<InheritQueryControl
setQueryAttribute={ setQueryAttributeBind }
query={ props.attributes.query }
query={ query }
/>
{ displayQueryControls ? (
<OrderByControl { ...props } />
) : null }
</ToolsPanel>

{ displayQueryControls ? (
<ToolsPanel
label={ __( 'Filters', 'woo-gutenberg-products-block' ) }
resetAll={ ( resetAllFilters: ( () => void )[] ) => {
setQueryAttribute( props, DEFAULT_FILTERS );
resetAllFilters.forEach( ( resetFilter ) =>
resetFilter()
);
} }
className="wc-block-editor-product-collection-inspector-toolspanel__filters"
>
<OnSaleControl { ...props } />
<StockStatusControl { ...props } />
<KeywordControl { ...props } />
<AttributesControl
woocommerceAttributes={
query.woocommerceAttributes || []
}
setQueryAttribute={ setQueryAttributeBind }
/>
<TaxonomyControls
setQueryAttribute={ setQueryAttributeBind }
query={ query }
/>
</ToolsPanel>
) : null }
</InspectorControls>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { isSiteEditorPage } from '@woocommerce/utils';
import { usePrevious } from '@woocommerce/base-hooks';
import { select } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import {
ToggleControl,
// @ts-expect-error Using experimental features
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import { ProductCollectionQuery } from '../types';
import { DEFAULT_QUERY } from '../constants';
import { getDefaultValueOfInheritQueryFromTemplate } from '../utils';

const label = __(
'Inherit query from template',
'woo-gutenberg-products-block'
);

interface InheritQueryControlProps {
setQueryAttribute: ( value: Partial< ProductCollectionQuery > ) => void;
query: ProductCollectionQuery | undefined;
}

const InheritQueryControl = ( {
setQueryAttribute,
query,
}: InheritQueryControlProps ) => {
const inherit = query?.inherit;
const editSiteStore = select( 'core/edit-site' );

const queryObjectBeforeInheritEnabled = usePrevious(
query,
( value?: ProductCollectionQuery ) => {
return value?.inherit === false;
}
);

const defaultValue = useMemo(
() => getDefaultValueOfInheritQueryFromTemplate(),
[]
);

// Hide the control if not in site editor.
const isSiteEditor = isSiteEditorPage( editSiteStore );
if ( ! isSiteEditor ) {
return null;
}

return (
<ToolsPanelItem
label={ label }
hasValue={ () => inherit !== defaultValue }
isShownByDefault
onDeselect={ () => {
setQueryAttribute( {
inherit: null,
} );
} }
>
<ToggleControl
label={ label }
help={ __(
'Toggle to use the global query context that is set with the current template, such as an archive or search. Disable to customize the settings independently.',
'woo-gutenberg-products-block'
) }
checked={ !! inherit }
onChange={ ( newInherit ) => {
if ( newInherit ) {
// If the inherit is enabled, we want to reset the query to the default.
setQueryAttribute( {
...DEFAULT_QUERY,
inherit: newInherit,
} );
} else {
// If the inherit is disabled, we want to reset the query to the previous query before the inherit was enabled.
setQueryAttribute( {
...DEFAULT_QUERY,
...queryObjectBeforeInheritEnabled,
inherit: newInherit,
} );
}
} }
/>
</ToolsPanelItem>
);
};

export default InheritQueryControl;
2 changes: 1 addition & 1 deletion assets/js/blocks/product-collection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface ProductCollectionDisplayLayout {
export interface ProductCollectionQuery {
author: string;
exclude: string[];
inherit: boolean;
inherit: boolean | null;
offset: number;
order: TProductCollectionOrder;
orderBy: TProductCollectionOrderBy;
Expand Down
25 changes: 25 additions & 0 deletions assets/js/blocks/product-collection/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { BlockEditProps } from '@wordpress/blocks';
import { select } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -26,3 +27,27 @@ export function setQueryAttribute(
},
} );
}

export function getDefaultValueOfInheritQueryFromTemplate(): boolean {
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',
];

const editSiteStore = select( 'core/edit-site' );
const currentTemplateId = editSiteStore?.getEditedPostId() as string;

/**
* Set inherit value when Product Collection block is first added to the page.
* We want inherit value to be true when block is added to ARCHIVE_PRODUCT_TEMPLATES
* and false when added to somewhere else.
*/
const initialValue = currentTemplateId
? ARCHIVE_PRODUCT_TEMPLATES.includes( currentTemplateId )
: false;

return initialValue;
}
2 changes: 1 addition & 1 deletion src/BlockTypes/ProductCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function ( $carry, $item ) {
);

return array(
// phpcs:ignore WordPress.DB.SlowDBQuery
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => array_values( $grouped_attributes ),
);
}
Expand Down