-
Notifications
You must be signed in to change notification settings - Fork 219
Product Collection: Add stock status filter #9580
Product Collection: Add stock status filter #9580
Conversation
- `InspectorControls` from './inspector-controls' is now imported in `edit.tsx` and used in the returned JSX of `Edit` function. - A new file `columns-control.tsx` is added under 'product-collection' block's 'inspector-controls' directory which exports a `ColumnsControl` component. This component uses `RangeControl` from '@wordpress/components' to control the number of columns in the product collection display layout when the layout type is 'flex'. - The types file (`types.ts`) for 'product-collection' block is updated. The `Attributes` interface is renamed to `ProductCollectionAttributes` and the `ProductCollectionContext` interface is removed. The `ProductCollectionAttributes` now includes 'queryContext', 'templateSlug', and 'displayLayout' properties.
This commit simplifies the fallback return value of the ColumnsControl component. Instead of returning an empty fragment (<> </>), it now returns null when the condition isn't met. This change improves readability and aligns with best practices for conditional rendering in React.
This commit adds a new 'Order By' control to the product collection inspector. The control allows users to specify the order of products in a collection by various attributes such as title and date. To support this, a new component 'OrderByControl' has been created and included in the product collection inspector. Additionally, the types for 'order' and 'orderBy' attributes have been updated and exported for reuse.
…ocks into 9359-product-collection-editor-settings-order-by
The main changes include: 1. Added a new property 'isProductCollectionBlock' in the block.json to denote if a block is a product collection block. 2. In the ProductCollection PHP class, a new initialization function has been defined to hook into the WordPress lifecycle, register the block, and update the query based on this block. 3. Added methods to manage query parameters for both frontend rendering and the Editor. 4. Expanded allowed 'collection_params' for the REST API to include custom 'orderby' values. 5. Defined a function to build the query based on block attributes, filters, and global WP_Query. 6. Created utility functions to handle complex query operations such as merging queries, handling custom sort values, and merging arrays recursively. These improvements allow for more flexible and robust handling of product collections in both the front-end display and the WordPress editor. It also extends support for custom 'orderby' values in the REST API, which allows for more advanced sorting options in product collections.
…ocks into 9361-product-collection-filters-on-sale
…ction block This commit introduces several changes to the product collection block. - First, it adds a new 'on sale' filter that can be used to display only the products that are currently on sale. - It also refactors the settings management in the product collection block to use the experimental ToolsPanel component from WordPress, which provides a more flexible and intuitive way to manage block settings. - It moves the 'Columns' control into the ToolsPanel, along with the 'Order by' control. - A new utility function `setQueryAttribute` is introduced to simplify setting nested query parameters. - The structure of the `ProductCollectionAttributes` and `ProductCollectionQuery` types have been adjusted to accommodate the changes. - Finally, it makes corresponding changes in the PHP part to handle the new 'on sale' query parameter. This should enhance the flexibility and user-friendliness of the product collection block.
This commit introduces a stock status filter to the WooCommerce product collection block. The changes include: 1. Added the ability to filter products based on their stock status within the 'product-collection' block. A new stock status control is created within the inspector-controls of the block. 2. A new 'get_stock_status_query' function is introduced in 'ProductCollection.php' which returns a query for products depending on their stock status. Please note that the stock status filter will only appear in the experimental build for now.
The release ZIP for this PR is accessible via:
Script Dependencies ReportThe
This comment was automatically generated by the TypeScript Errors Report
assets/js/blocks/product-collection/inspector-controls/stock-status-control.tsx
|
Size Change: +355 B (0%) Total Size: 1.09 MB
ℹ️ View Unchanged
|
This commit refactors the Stock Status control. The changes aim to improve the code organization and make the behavior of the component more explicit. The key modifications are: 1. Moved stock status related constants and functions from `inspector-controls/utils.tsx` to `inspector-controls/constants.ts`. This is done to ensure that all constants and similar utility functions are organized in one place. 2. Updated `product-collection/index.tsx` to import `getDefaultStockStatuses` from `inspector-controls/constants` instead of `inspector-controls/utils`. 3. Updated `stock-status-control.tsx` to determine whether the stock status has value or not by comparing with the default stock statuses using `fastDeepEqual`. If the stock status control is deselected, it resets the stock status to the default statuses. These changes do not introduce any new functionalities, but improve the readability and maintainability of the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good and it's working as expected. I left some comments inline, but only the PHP warning is a blocking one IMO, so pre-approving (but feel free to ask for a re-review if you want).
src/BlockTypes/ProductCollection.php
Outdated
* @param array $stock_statii An array of acceptable stock statii. | ||
* @return array | ||
*/ | ||
private function get_stock_status_query( $stock_statii ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about statii, I guess that's the plural of status, no? I think in the codebase we use statuses
more often than statii
, so I would rename this variable to $stock_statuses
if possible to keep it consistent. Not a strong opinion, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$params['orderby']['enum'] = array_unique( array_merge( $original_enum, $this->custom_order_opts ) ); | ||
return $params; | ||
private function get_final_query_args( $common_query_values, $query ) { | ||
$orderby_query = $query['orderby'] ? $this->get_custom_orderby_query( $query['orderby'] ) : []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes a PHP warning in the frontend if orderby
is not defined:
Warning: Undefined array key "orderby" in /wp-content/plugins/woocommerce-blocks/src/BlockTypes/ProductCollection.php on line 100
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* If all stock statuses are selected except 'outofstock', we use the | ||
* product visibility query to filter out out of stock products. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to say that, as a user, I would find this a bit confusing because the block settings and WC settings can contradict each other. IMO block settings should take priority over WooCommerce settings. So if the Hide out of stock items from the catalog option is checked but I add a Product Collection block selecting to display products with all stock statuses, I would expect all products to appear, ignoring the Hide out of stock items from the catalog option.
In other words, I think the Hide out of stock items from the catalog option should affect which stock status options are selected by default when inserting the block, but they shouldn't affect how the block works once the block attributes have been set.
But... I don't have a strong opinion so I'm happy to change my mind. Also, I don't think it's a blocker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Aljullu,
I think the Hide out of stock items from the catalog option should affect which stock status options are selected by default when inserting the block, but they shouldn't affect how the block works once the block attributes have been set.
I just want to confirm that currently, when we insert the block, we do take the "Hide out of stock items from the catalog" setting into consideration. This is implemented here.
Also, I agree that it makes sense to ignore the "Hide out of stock items from the catalog" setting if we allow merchants to explicitly set stock statuses in the Product Collection block.
PS: I will go ahead and merge this PR for now. We can continue our discussion here. If we decide to ignore the global "Hide out of stock items from the catalog" setting, I will create a new PR for that.
CC: @nerrad @tjcafferkey
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want to confirm that currently, when we insert the block, we do take the "Hide out of stock items from the catalog" setting into consideration. This is implemented here.
Oh right, I missed it! That's nice.
This was happening because of this issue: WordPress/gutenberg#7342 Therefore, I had to use `useEffect` to set the default values of the attributes. Here is the list of changes I made: 1. Removed default values from `block.json` for `query`, `tagName`, and `displayLayout`. 2. Moved the default values to `constants.ts` and created a new object `DEFAULT_ATTRIBUTES` to store them. 3. Relocated `constants.ts` from `inspector-controls` to the parent directory. 4. Refactored `edit.tsx` to use `DEFAULT_ATTRIBUTES` from `constants.ts` to set default attributes using `useEffect`. 5. Removed the attributes assignment from `registerBlockType` in `index.tsx`. 6. Updated `columns-control.tsx`, `index.tsx`, `order-by-control.tsx`, and `stock-status-control.tsx` to import from the relocated `constants.ts`. 7. Updated `ProductCollectionAttributes` and `ProductCollectionQuery` in `types.ts` to include `tagName` and `isProductCollectionBlock` respectively. 8. Modified `ProductCollection.php` to match the updated `orderBy` key in the query parameter. This refactor enhances the readability of the code and reduces duplication by keeping all constants and default values in one place.
This commit replaces all instances of 'statii' with the correct term 'statuses' in the context of handling stock status. This change affects three files: 1. `assets/js/blocks/product-collection/inspector-controls/stock-status-control.tsx` - The term is corrected in a comment block. 2. `assets/js/blocks/product-collection/types.ts` - Updated the name of a variable in the `ProductCollectionQuery` interface. 3. `src/BlockTypes/ProductCollection.php` - Here, the term is replaced in several locations including variable names, comments and the method `get_stock_status_query`. This commit helps improve code readability and consistency across the repository.
This PR adds
Stock status
control to the Product collection block.Fixes #9362
Testing
Product Collection
block selected, check the sidebar for theStock status
control. Ensure that it is displayed as shown below:On backorder
is the selected option, only products with theOn backorder
status should be visible in the Editor.Reset
orReset all
button, as shown below:WooCommerce Visibility
Changelog