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 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Product Collection: Add support for filtering products by featured st…
…atus (#11522) * Add support for filtering products by featured status - Added `featured` attribute to `ProductCollectionQuery` type to enable filtering by featured status. - Implemented `FeaturedProductsControl` to provide a toggle option in the inspector controls. - Integrated `FeaturedProductsControl` into `ProductCollectionInspectorControls`. - Added `get_featured_query` function in `ProductCollection` class to generate query for fetching featured products. - Updated existing functions and queries in `ProductCollection` class to support featured products filtering. * Revert changes to composer.lock * Refactor handling of 'featured' parameter This commit makes the handling of the 'featured' parameter consistent in the ProductCollection class. Previously, the 'featured' parameter was being type-casted to boolean, which was not necessary and could lead to incorrect results. Now, the 'featured' parameter is used directly without type-casting, and the check for 'featured' products in the get_featured_query method has been updated accordingly. This ensures that the 'featured' parameter is handled consistently and correctly throughout the class. * Handle undefined 'featured' index This commit adds null coalescing operator to handle the case when 'featured' index is not set in the $query array. This prevents potential PHP notices or errors that may arise when trying to access an undefined index.
- Loading branch information
1 parent
0cd5468
commit d879490
Showing
5 changed files
with
94 additions
and
1 deletion.
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
52 changes: 52 additions & 0 deletions
52
assets/js/blocks/product-collection/inspector-controls/featured-products-control.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,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
BaseControl, | ||
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 { QueryControlProps } from '../types'; | ||
|
||
const FeaturedProductsControl = ( props: QueryControlProps ) => { | ||
const { query, setQueryAttribute } = props; | ||
|
||
return ( | ||
<ToolsPanelItem | ||
label={ __( 'Featured', 'woo-gutenberg-products-block' ) } | ||
hasValue={ () => query.featured === true } | ||
onDeselect={ () => { | ||
setQueryAttribute( { | ||
featured: false, | ||
} ); | ||
} } | ||
> | ||
<BaseControl | ||
id="product-collection-featured-products-control" | ||
label={ __( 'Featured', 'woo-gutenberg-products-block' ) } | ||
> | ||
<ToggleControl | ||
label={ __( | ||
'Show only featured products', | ||
'woo-gutenberg-products-block' | ||
) } | ||
checked={ query.featured || false } | ||
onChange={ ( featured ) => { | ||
setQueryAttribute( { | ||
featured, | ||
} ); | ||
} } | ||
/> | ||
</BaseControl> | ||
</ToolsPanelItem> | ||
); | ||
}; | ||
|
||
export default FeaturedProductsControl; |
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