Skip to content

Commit

Permalink
[Product Query] Add support for the Filter Products By Stock block (w…
Browse files Browse the repository at this point in the history
…oocommerce#7251)

* Product Query: Fix pagination issue

* Product Query - Add support for the Filter By Price Block woocommerce#6790

Product Query - Add support for the Filter By Price Block

* fix query relation

* fix on sale query

* Product Query - Add support for the Filter By Attributes block woocommerce#6790

Product Query - Add support for the Filter By Attributes block

* fix bugged pagination and on-sale filter after refactor

* address feedback

* Product Query - Add support for the Filter By Stock Block woocommerce#6790

Product Query - Add support for the Filter By Stock Block

* address feedback
  • Loading branch information
gigitux authored and senadir committed Nov 12, 2022
1 parent 55099e3 commit 2d72044
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
72 changes: 65 additions & 7 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,9 @@ private function get_stock_status_query( $stock_statii ) {
/**
* 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 ) {

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 @@ -187,8 +185,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 @@ -235,8 +255,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 @@ -344,6 +365,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::get_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
10 changes: 9 additions & 1 deletion src/BlockTypes/StockFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class StockFilter extends AbstractBlock {
*
* @var string
*/
protected $block_name = 'stock-filter';
protected $block_name = 'stock-filter';
const STOCK_STATUS_QUERY_VAR = 'filter_stock_status';

/**
* Extra data passed through from server to client for block.
Expand All @@ -25,4 +26,11 @@ protected function enqueue_data( array $stock_statuses = [] ) {
$this->asset_data_registry->add( 'hideOutOfStockItems', 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ), true );

}

/**
* Get Stock status query variables values.
*/
public static function get_stock_status_query_var_values() {
return array_keys( wc_get_product_stock_status_options() );
}
}

0 comments on commit 2d72044

Please sign in to comment.