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

Commit

Permalink
[Product Query] Fix filter block data counter (#7257)
Browse files Browse the repository at this point in the history
* Product Query: Fix pagination issue

* Product Query - Add support for the Filter By Price Block #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 #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 #6790

Product Query - Add support for the Filter By Stock Block

* Fix filter blocks: the data (e.g: max price or stock-status) match the variation #7245

fix filter blocks: the data (e.g: max price or stock-status) match the variation

* disable phcs rules on top of the file

* replace parameter name

* fix eslint error
  • Loading branch information
gigitux authored Nov 2, 2022
1 parent 195d1fd commit 84e1bbd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { useState, useEffect, useMemo } from '@wordpress/element';
import { useDebounce } from 'use-debounce';
import { sortBy } from 'lodash';
import { isEmpty, sortBy } from 'lodash';
import { useShallowEqual } from '@woocommerce/base-hooks';
import { objectHasProp } from '@woocommerce/types';

Expand Down Expand Up @@ -47,6 +47,7 @@ interface UseCollectionDataProps {
queryStock?: boolean;
queryRating?: boolean;
queryState: Record< string, unknown >;
productIds?: number[];
}

export const useCollectionData = ( {
Expand All @@ -55,6 +56,7 @@ export const useCollectionData = ( {
queryStock,
queryRating,
queryState,
productIds,
}: UseCollectionDataProps ) => {
let context = useQueryStateContext();
context = `${ context }-collection-data`;
Expand Down Expand Up @@ -163,6 +165,7 @@ export const useCollectionData = ( {
per_page: undefined,
orderby: undefined,
order: undefined,
...( ! isEmpty( productIds ) && { include: productIds } ),
...collectionDataQueryVars,
},
shouldSelect: debouncedShouldSelect,
Expand Down
5 changes: 5 additions & 0 deletions assets/js/blocks/attribute-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ const AttributeFilterBlock = ( {
isString
);

const productIds = isEditor
? []
: getSettingWithCoercion( 'product_ids', [], Array.isArray );

const [ hasSetFilterDefaultsFromUrl, setHasSetFilterDefaultsFromUrl ] =
useState( false );

Expand Down Expand Up @@ -158,6 +162,7 @@ const AttributeFilterBlock = ( {
...queryState,
attributes: filterAvailableTerms ? queryState.attributes : null,
},
productIds,
} );

/**
Expand Down
5 changes: 5 additions & 0 deletions assets/js/blocks/price-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ const PriceFilterBlock = ( {
isBoolean
);

const productIds = isEditor
? []
: getSettingWithCoercion( 'product_ids', [], Array.isArray );

const [ hasSetFilterDefaultsFromUrl, setHasSetFilterDefaultsFromUrl ] =
useState( false );

Expand All @@ -108,6 +112,7 @@ const PriceFilterBlock = ( {
const { results, isLoading } = useCollectionData( {
queryPrices: true,
queryState,
productIds,
} );

const currency = getCurrencyFromPriceResponse(
Expand Down
5 changes: 5 additions & 0 deletions assets/js/blocks/stock-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const StockStatusFilterBlock = ( {
{}
);

const productIds = isEditor
? []
: getSettingWithCoercion( 'product_ids', [], Array.isArray );

const STOCK_STATUS_OPTIONS = useRef(
getSetting( 'hideOutOfStockItems', false )
? otherStockStatusOptions
Expand Down Expand Up @@ -101,6 +105,7 @@ const StockStatusFilterBlock = ( {
useCollectionData( {
queryStock: true,
queryState,
productIds,
} );

/**
Expand Down
66 changes: 50 additions & 16 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;

// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_tax_query
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query

/**
* ProductQuery class.
*/
Expand Down Expand Up @@ -73,6 +76,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' ),
Expand Down Expand Up @@ -114,10 +118,7 @@ public function build_query( $query ) {
'orderby' => $query['orderby'],
'order' => $query['order'],
'offset' => $query['offset'],
// Ignoring the warning of not using meta queries.
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(),
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => array(),
);

Expand All @@ -130,19 +131,57 @@ 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'];

// 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'];
// 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;
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,
'meta_query' => array(),
'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 $a The first query.
* @param array $b The second query.
* @return array
*/
private function merge_queries( $a, $b ) {
$a['post__in'] = ( isset( $b['post__in'] ) && ! empty( $b['post__in'] ) ) ? $this->intersect_arrays_when_not_empty( $a['post__in'], $b['post__in'] ) : $a['post__in'];
$a['meta_query'] = ( isset( $b['meta_query'] ) && ! empty( $b['meta_query'] ) ) ? array_merge( $a['meta_query'], array( $b['meta_query'] ) ) : $a['meta_query'];
$a['tax_query'] = ( isset( $b['tax_query'] ) && ! empty( $b['tax_query'] ) ) ? array_merge( $a['tax_query'], array( $b['tax_query'] ) ) : $a['tax_query'];

return $a;
}

/**
* Return a query for on sale products.
*
Expand All @@ -162,7 +201,6 @@ private function get_on_sale_products_query() {
*/
private function get_stock_status_query( $stock_statii ) {
return array(
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
'key' => '_stock_status',
'value' => (array) $stock_statii,
Expand Down Expand Up @@ -305,8 +343,6 @@ private function get_filter_by_price_query() {
}

return array(
// Ignoring the warning of not using meta queries.
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
'relation' => 'AND',
$max_price_query,
Expand Down Expand Up @@ -356,8 +392,6 @@ function( $acc, $query_args ) {
}

return array(
// Ignoring the warning of not using meta queries.
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'tax_query' => array(
'relation' => 'AND',
$queries,
Expand Down

0 comments on commit 84e1bbd

Please sign in to comment.