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

Commit

Permalink
Product Query - Add support for the Filter By Stock Block #6790
Browse files Browse the repository at this point in the history
Product Query - Add support for the Filter By Stock Block
  • Loading branch information
gigitux committed Sep 29, 2022
1 parent 80a0dbf commit 5be22b9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
76 changes: 68 additions & 8 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function( $acc, $query ) {
$acc['meta_query'] = isset( $query['meta_query'] ) ? array_merge( $acc['meta_query'], array( $query['meta_query'] ) ) : $acc['meta_query'];
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
$acc['tax_query'] = isset( $query['tax_query'] ) ? array_merge( $acc['tax_query'], array( $query['tax_query'] ) ) : $acc['tax_query'];

return $acc;
},
$common_query_values
Expand All @@ -146,13 +147,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 @@ -161,8 +160,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 @@ -209,8 +230,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 @@ -315,6 +337,44 @@ 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 when both are not 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' );

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

0 comments on commit 5be22b9

Please sign in to comment.