-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
…e variation #7245 fix filter blocks: the data (e.g: max price or stock-status) match the variation
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ public function update_query( $pre_render, $parsed_block ) { | |
// Set this so that our product filters can detect if it's a PHP template. | ||
$this->asset_data_registry->add( 'has_filterable_products', true, true ); | ||
$this->asset_data_registry->add( 'is_rendering_php_template', true, true ); | ||
$this->asset_data_registry->add( 'product_ids', $this->get_products_ids_by_attributes( $parsed_block ), true ); | ||
add_filter( | ||
'query_loop_block_query_vars', | ||
array( $this, 'build_query' ), | ||
|
@@ -119,19 +120,62 @@ public function build_query( $query ) { | |
$queries_by_filters | ||
), | ||
function( $acc, $query ) { | ||
$acc['post__in'] = isset( $query['post__in'] ) ? $this->intersect_arrays_when_not_empty( $acc['post__in'], $query['post__in'] ) : $acc['post__in']; | ||
return $this->merge_queries( $acc, $query ); | ||
}, | ||
$common_query_values | ||
); | ||
} | ||
|
||
/** | ||
* Return the product ids based on the attributes. | ||
* | ||
* @param array $parsed_block The block being rendered. | ||
* @return array | ||
*/ | ||
private function get_products_ids_by_attributes( $parsed_block ) { | ||
$queries_by_attributes = $this->get_queries_by_attributes( $parsed_block ); | ||
|
||
$query = array_reduce( | ||
$queries_by_attributes, | ||
function( $acc, $query ) { | ||
return $this->merge_queries( $acc, $query ); | ||
}, | ||
array( | ||
'post_type' => 'product', | ||
'post__in' => array(), | ||
'post_status' => 'publish', | ||
'posts_per_page' => -1, | ||
// Ignoring the warning of not using meta queries. | ||
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query | ||
$acc['meta_query'] = isset( $query['meta_query'] ) ? array_merge( $acc['meta_query'], array( $query['meta_query'] ) ) : $acc['meta_query']; | ||
'meta_query' => array(), | ||
// 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 | ||
'tax_query' => array(), | ||
) | ||
); | ||
|
||
$products = new \WP_Query( $query ); | ||
$post_ids = wp_list_pluck( $products->posts, 'ID' ); | ||
|
||
return $post_ids; | ||
} | ||
|
||
/** | ||
* Merge in the first parameter the keys "post_in", "meta_query" and "tax_query" of the second parameter. | ||
* | ||
* @param array $query1 The first query. | ||
* @param array $query2 The second query. | ||
* @return array | ||
*/ | ||
private function merge_queries( $query1, $query2 ) { | ||
$query1['post__in'] = isset( $query2['post__in'] ) ? $this->intersect_arrays_when_not_empty( $query1['post__in'], $query2['post__in'] ) : $query1['post__in']; | ||
// Ignoring the warning of not using meta queries. | ||
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query | ||
$query1['meta_query'] = isset( $query2['meta_query'] ) ? array_merge( $query1['meta_query'], array( $query2['meta_query'] ) ) : $query1['meta_query']; | ||
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query | ||
$query1['tax_query'] = isset( $query2['tax_query'] ) ? array_merge( $query1['tax_query'], array( $query2['tax_query'] ) ) : $query1['tax_query']; | ||
|
||
return $query1; | ||
|
||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gigitux
Author
Contributor
|
||
} | ||
|
||
|
||
|
I have a few comments about this function.
$a
and$b
, than$query1
and$query2
in these kinds of scenarios, usually.phpcs:ignore
comment with just onephpcs:disable
at the beginning of the function? Or perhaps even at the beginning of the file, given we are going to be usingmeta_query
a bunch here.