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

Product Collection: Add stock status filter #9580

Merged
merged 20 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 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
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
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
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
255b164
Resolve conflicts
imanish003 May 25, 2023
d1e29ef
Fix: Default values of attributes not saving as serialized block comment
imanish003 May 26, 2023
0c30f5b
Replace usage of 'statii' with 'statuses' in stock status handling
imanish003 May 26, 2023
c99646e
Merge branch 'trunk' into 9362-product-collection-filters-stock-status
imanish003 May 26, 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
8 changes: 8 additions & 0 deletions assets/js/blocks/product-collection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ import edit from './edit';
import save from './save';
import icon from './icon';
import './variations';
import { getDefaultStockStatuses } from './inspector-controls/constants';

if ( isExperimentalBuild() ) {
registerBlockType( metadata, {
icon,
attributes: {
...metadata.attributes,
query: {
...metadata.attributes.query,
default: {
...metadata.attributes.query.default,
woocommerceStockStatus: getDefaultStockStatuses(),
},
},
},
edit,
save,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* External dependencies
*/
import { getSetting } from '@woocommerce/settings';
import { objectOmit } from '@woocommerce/utils';

/**
* Internal dependencies
*/
Expand All @@ -10,8 +16,25 @@ import {

const defaultQuery = blockJson.attributes.query.default;

export const STOCK_STATUS_OPTIONS = getSetting< Record< string, string > >(
'stockStatusOptions',
[]
);

const GLOBAL_HIDE_OUT_OF_STOCK = getSetting< boolean >(
'hideOutOfStockItems',
false
);

export const getDefaultStockStatuses = () => {
return GLOBAL_HIDE_OUT_OF_STOCK
? Object.keys( objectOmit( STOCK_STATUS_OPTIONS, 'outofstock' ) )
: Object.keys( STOCK_STATUS_OPTIONS );
};

export const DEFAULT_FILTERS = {
woocommerceOnSale: defaultQuery.woocommerceOnSale,
woocommerceStockStatus: getDefaultStockStatuses(),
};

export const getDefaultSettings = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import OrderByControl from './order-by-control';
import OnSaleControl from './on-sale-control';
import { setQueryAttribute } from './utils';
import { DEFAULT_FILTERS, getDefaultSettings } from './constants';
import StockStatusControl from './stock-status-control';

const ProductCollectionInspectorControls = (
props: BlockEditProps< ProductCollectionAttributes >
Expand All @@ -45,6 +46,7 @@ const ProductCollectionInspectorControls = (
} }
>
<OnSaleControl { ...props } />
<StockStatusControl { ...props } />
</ToolsPanel>
</InspectorControls>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
* Internal dependencies
*/
import { ProductCollectionAttributes } from '../types';
import { setQueryAttribute } from './utils';

const OnSaleControl = (
props: BlockEditProps< ProductCollectionAttributes >
Expand All @@ -23,14 +24,11 @@ const OnSaleControl = (
return (
<ToolsPanelItem
label={ __( 'On Sale', 'woo-gutenberg-products-block' ) }
hasValue={ () => query.woocommerceOnSale }
hasValue={ () => query.woocommerceOnSale === true }
isShownByDefault
onDeselect={ () => {
props.setAttributes( {
query: {
...query,
woocommerceOnSale: false,
},
setQueryAttribute( props, {
woocommerceOnSale: false,
} );
} }
>
Expand All @@ -41,11 +39,8 @@ const OnSaleControl = (
) }
checked={ query.woocommerceOnSale || false }
onChange={ ( woocommerceOnSale ) => {
props.setAttributes( {
query: {
...query,
woocommerceOnSale,
},
setQueryAttribute( props, {
woocommerceOnSale,
} );
} }
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { BlockEditProps } from '@wordpress/blocks';
import fastDeepEqual from 'fast-deep-equal/es6';
import {
FormTokenField,
// @ts-expect-error Using experimental features
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import { ProductCollectionAttributes } from '../types';
import { setQueryAttribute } from './utils';
import { STOCK_STATUS_OPTIONS, getDefaultStockStatuses } from './constants';

/**
* Gets the id of a specific stock status from its text label
*
* In theory, we could use a `saveTransform` function on the
* `FormFieldToken` component to do the conversion. However, plugins
* can add custom stock statii which don't conform to our naming
* conventions.
*/
function getStockStatusIdByLabel( statusLabel: FormTokenField.Value ) {
const label =
typeof statusLabel === 'string' ? statusLabel : statusLabel.value;

return Object.entries( STOCK_STATUS_OPTIONS ).find(
( [ , value ] ) => value === label
)?.[ 0 ];
}

const StockStatusControl = (
props: BlockEditProps< ProductCollectionAttributes >
) => {
const { query } = props.attributes;
return (
<ToolsPanelItem
label={ __( 'Stock status', 'woo-gutenberg-products-block' ) }
hasValue={ () =>
! fastDeepEqual(
query.woocommerceStockStatus,
getDefaultStockStatuses()
)
}
onDeselect={ () => {
setQueryAttribute( props, {
woocommerceStockStatus: getDefaultStockStatuses(),
} );
} }
isShownByDefault
>
<FormTokenField
label={ __( 'Stock status', 'woo-gutenberg-products-block' ) }
onChange={ ( statusLabels ) => {
const woocommerceStockStatus = statusLabels
.map( getStockStatusIdByLabel )
.filter( Boolean ) as string[];

setQueryAttribute( props, {
woocommerceStockStatus,
} );
} }
suggestions={ Object.values( STOCK_STATUS_OPTIONS ) }
validateInput={ ( value: string ) =>
Object.values( STOCK_STATUS_OPTIONS ).includes( value )
}
value={
query?.woocommerceStockStatus?.map(
( key ) => STOCK_STATUS_OPTIONS[ key ]
) || []
}
__experimentalExpandOnFocus={ true }
/>
</ToolsPanelItem>
);
};

export default StockStatusControl;
14 changes: 14 additions & 0 deletions assets/js/blocks/product-collection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ export interface ProductCollectionQuery {
sticky: string;
taxQuery: string;
woocommerceOnSale: boolean;
/**
* Filter products by their stock status.
*
* Will generate the following `meta_query`:
*
* ```
* array(
* 'key' => '_stock_status',
* 'value' => (array) $stock_statii,
* 'compare' => 'IN',
* ),
* ```
*/
woocommerceStockStatus?: string[];
}

export type TProductCollectionOrder = 'asc' | 'desc';
Expand Down