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

[Product Query] Add support for the Filter Products By Stock block #7251

Merged
merged 26 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
16ea028
Product Query: Fix pagination issue
gigitux Sep 12, 2022
f19fd99
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Sep 14, 2022
816ffe9
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Sep 15, 2022
6e93dd0
Product Query - Add support for the Filter By Price Block #6790
gigitux Sep 15, 2022
b243528
fix query relation
gigitux Sep 16, 2022
e55a41d
fix on sale query
gigitux Sep 16, 2022
341d47b
Merge branch 'trunk' into add/price_filter_support
gigitux Sep 16, 2022
1fd15a8
Merge branch 'trunk' into add/price_filter_support
gigitux Sep 20, 2022
926f62c
Product Query - Add support for the Filter By Attributes block #6790
gigitux Sep 20, 2022
3176679
Merge branch 'trunk' into add/attribute_filter_support
gigitux Sep 21, 2022
1d56a2e
fix bugged pagination and on-sale filter after refactor
gigitux Sep 21, 2022
c9839f2
Merge branch 'add/price_filter_support' of https://github.com/woocomm…
gigitux Sep 21, 2022
9065ab3
Merge branch 'add/price_filter_support' of https://github.com/woocomm…
gigitux Sep 21, 2022
0be510e
Merge branch 'add/attribute_filter_support' of https://github.com/woo…
gigitux Sep 21, 2022
6f326eb
Merge ranch 'trunk' of https://github.com/woocommerce/woocommerce-blo…
gigitux Sep 22, 2022
5622639
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Sep 28, 2022
c22d149
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Sep 28, 2022
80a0dbf
address feedback
gigitux Sep 28, 2022
5be22b9
Product Query - Add support for the Filter By Stock Block #6790
gigitux Sep 29, 2022
2e5f59e
Merge branch 'trunk' into add/stock_filter_support
gigitux Sep 29, 2022
b093589
Merge branch 'trunk' into add/stock_filter_support
gigitux Oct 7, 2022
9ea5689
Merge branch 'trunk' into add/stock_filter_support
gigitux Oct 10, 2022
e879ae6
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Oct 26, 2022
027ab12
Merge branch 'add/stock_filter_support' of https://github.com/woocomm…
gigitux Oct 26, 2022
9909197
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Nov 2, 2022
d23d6a0
address feedback
gigitux Nov 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 66 additions & 8 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,11 @@ private function get_on_sale_products_query() {
}

/**
* Set the query vars that are used by filter blocks.
* Return all the query vars that are used by filter blocks.
*
* @param array $public_query_vars Public query vars.
* @return array
*/
public function set_query_vars( $public_query_vars ) {

private function get_query_vars_from_filter_blocks() {
$attributes_filter_query_args = array_reduce(
array_values( $this->get_filter_by_attributes_query_vars() ),
function( $acc, $array ) {
Expand All @@ -171,8 +169,30 @@ function( $acc, $array ) {
array()
);

$price_filter_query_args = array( PriceFilter::MIN_PRICE_QUERY_VAR, PriceFilter::MAX_PRICE_QUERY_VAR );
return array_merge( $public_query_vars, $price_filter_query_args, $attributes_filter_query_args );
return array(
'price_filter_query_args' => array( PriceFilter::MIN_PRICE_QUERY_VAR, PriceFilter::MAX_PRICE_QUERY_VAR ),
'stock_filter_query_args' => array( StockFilter::STOCK_STATUS_QUERY_VAR ),
'attributes_filter_query_args' => $attributes_filter_query_args,
);

}

/**
* Set the query vars that are used by filter blocks.
*
* @param array $public_query_vars Public query vars.
* @return array
*/
public function set_query_vars( $public_query_vars ) {
$query_vars = $this->get_query_vars_from_filter_blocks();

return array_reduce(
array_values( $query_vars ),
function( $acc, $query_vars_filter_block ) {
return array_merge( $query_vars_filter_block, $acc );
},
$public_query_vars
);
}

/**
Expand Down Expand Up @@ -219,8 +239,9 @@ function( $acc, $attribute ) {
*/
private function get_queries_by_applied_filters() {
return array(
'price_filter' => $this->get_filter_by_price_query(),
'attributes_filter' => $this->get_filter_by_attributes_query(),
'price_filter' => $this->get_filter_by_price_query(),
'attributes_filter' => $this->get_filter_by_attributes_query(),
'stock_status_filter' => $this->get_filter_by_stock_status_query(),
);
}

Expand Down Expand Up @@ -325,6 +346,43 @@ function( $acc, $query_args ) {
);
}

/**
* Return a query that filters products by stock status.
*
* @return array
*/
private function get_filter_by_stock_status_query() {
$filter_stock_status_values = get_query_var( StockFilter::STOCK_STATUS_QUERY_VAR );

if ( empty( $filter_stock_status_values ) ) {
return array();
}

$filtered_stock_status_values = array_filter(
explode( ',', $filter_stock_status_values ),
function( $stock_status ) {
return in_array( $stock_status, StockFilter::STOCK_STATUS_QUERY_VAR_VALUES, true );
}
);

if ( empty( $filtered_stock_status_values ) ) {
return array();
}

return array(
// Ignoring the warning of not using meta queries.
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => $filtered_stock_status_values,
'operator' => 'IN',

),
),
);
}

/**
* Intersect arrays neither of them are empty, otherwise merge them.
*
Expand Down
4 changes: 3 additions & 1 deletion src/BlockTypes/StockFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class StockFilter extends AbstractBlock {
*
* @var string
*/
protected $block_name = 'stock-filter';
protected $block_name = 'stock-filter';
const STOCK_STATUS_QUERY_VAR = 'filter_stock_status';
const STOCK_STATUS_QUERY_VAR_VALUES = array( 'instock', 'outofstock', 'onbackorder' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the stock statuses returned by wc_get_product_stock_status_options() instead of hardcoding them? This way this will be compatible with any extension hooking into the woocommerce_product_stock_status_options filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I addressed the feedback with d23d6a0.


/**
* Extra data passed through from server to client for block.
Expand Down