This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Product Query: Add order by “best selling” as a preset (#7687)
* Add support for “Popular Presets” for PQ block This commits achieves the following: * Adds a section in the inspector control called “Popular Presets”, which contains a dropdown with popular presets. * Adds support for the first preset: “Best selling products”. By selecting this, users can sort products by total sales. * Switches the order of the custom inspector controls and the default Query Loop inspector controls: our controls will be now on top as per the latest design spec (see pdnLyh-2By-p2). * Restricts the allowed Query parameters to the sort orders we want to allow according to the latest design spec (disabling title and date). * Removes the core “Order By” dropdown. * Refactor `setCustomQueryAttribute` to `setQueryAttribute` because since a few iterations, our custom query attributes are not deeply nested anymore, and this function can be used for the normal query too. * Add back-end support for sorting by Best Selling via the Product Query block * Adds the `popularity` value as an allowed value for `orderby` on `product` REST API calls. * Handles the query differently if the `orderby` value is one among the custom ones.
- Loading branch information
1 parent
28fd155
commit 03da591
Showing
6 changed files
with
152 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
assets/js/blocks/product-query/inspector-controls/popular-presets.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { CustomSelectControl, PanelBody } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ProductQueryBlock, ProductQueryBlockQuery } from '../types'; | ||
import { setQueryAttribute } from '../utils'; | ||
|
||
const PRESETS = [ | ||
{ key: 'date/desc', name: __( 'Newest', 'woo-gutenberg-products-block' ) }, | ||
{ | ||
key: 'popularity/desc', | ||
name: __( 'Best Selling', 'woo-gutenberg-products-block' ), | ||
}, | ||
]; | ||
|
||
export function PopularPresets( props: ProductQueryBlock ) { | ||
const { query } = props.attributes; | ||
|
||
return ( | ||
<PanelBody | ||
className="woocommerce-product-query-panel__sort" | ||
title={ __( 'Popular Filters', 'woo-gutenberg-products-block' ) } | ||
initialOpen={ true } | ||
> | ||
<p> | ||
{ __( | ||
'Arrange products by popular pre-sets.', | ||
'woo-gutenberg-products-block' | ||
) } | ||
</p> | ||
<CustomSelectControl | ||
hideLabelFromVision={ true } | ||
label={ __( | ||
'Choose among these pre-sets', | ||
'woo-gutenberg-products-block' | ||
) } | ||
onChange={ ( option ) => { | ||
if ( ! option.selectedItem?.key ) return; | ||
|
||
const [ orderBy, order ] = option.selectedItem?.key?.split( | ||
'/' | ||
) as [ | ||
ProductQueryBlockQuery[ 'orderBy' ], | ||
ProductQueryBlockQuery[ 'order' ] | ||
]; | ||
|
||
setQueryAttribute( props, { order, orderBy } ); | ||
} } | ||
options={ PRESETS } | ||
value={ PRESETS.find( | ||
( option ) => | ||
option.key === `${ query.orderBy }/${ query.order }` | ||
) } | ||
/> | ||
</PanelBody> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters