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

[Product Query] Fix filter block data counter #7257

Merged
merged 36 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 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
b8bf17e
Fix filter blocks: the data (e.g: max price or stock-status) match th…
gigitux Sep 29, 2022
1bef270
Merge branch 'trunk' into fix/filter_display_data
gigitux Sep 29, 2022
b093589
Merge branch 'trunk' into add/stock_filter_support
gigitux Oct 7, 2022
c7e9773
Merge branch 'trunk' into fix/filter_display_data
gigitux Oct 7, 2022
b1b05f6
Merge branch 'trunk' into fix/filter_display_data
gigitux Oct 10, 2022
9ea5689
Merge branch 'trunk' into add/stock_filter_support
gigitux Oct 10, 2022
f20d7da
Merge branch 'trunk' into fix/filter_display_data
gigitux Oct 11, 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
0c45b07
Merge branch 'add/stock_filter_support' of https://github.com/woocomm…
gigitux Oct 27, 2022
c4a46c3
disable phcs rules on top of the file
gigitux Oct 27, 2022
a07778c
Merge branch 'trunk' into fix/filter_display_data
gigitux Oct 27, 2022
e95122d
replace parameter name
gigitux Oct 27, 2022
3c8e6d0
Merge branch 'fix/filter_display_data' of https://github.com/woocomme…
gigitux Oct 27, 2022
987d4a4
fix eslint error
gigitux Oct 27, 2022
a004b75
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
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
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