Skip to content

Commit

Permalink
Fix: show correct attribute and price filter data on the category/tag…
Browse files Browse the repository at this point in the history
… template (woocommerce#50341)
  • Loading branch information
normana10 authored Oct 2, 2024
1 parent 7bef8af commit 73e7d57
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
isBoolean,
isString,
objectHasProp,
isObject,
} from '@woocommerce/types';
import { Icon, chevronDown } from '@wordpress/icons';
import {
Expand Down Expand Up @@ -133,13 +134,21 @@ const AttributeFilterBlock = ( {
query: { orderby: attributeObject?.orderby || 'menu_order' },
} );

const backendQueryState = getSettingWithCoercion(
'queryState',
{},
isObject
);
const { results: filteredCounts, isLoading: filteredCountsLoading } =
useCollectionData( {
queryAttribute: {
taxonomy: attributeObject?.taxonomy || '',
queryType: blockAttributes.queryType,
},
queryState,
queryState: {
...backendQueryState,
...queryState,
},
isEditor,
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { changeUrl, getUrlParameter } from '@woocommerce/utils';
import {
CurrencyResponse,
isBoolean,
isObject,
isString,
objectHasProp,
} from '@woocommerce/types';
Expand Down Expand Up @@ -112,9 +113,17 @@ const PriceFilterBlock = ( {
const minPriceParam = getUrlParameter( 'min_price' );
const maxPriceParam = getUrlParameter( 'max_price' );
const [ queryState ] = useQueryStateByContext();
const backendQueryState = getSettingWithCoercion(
'queryState',
{},
isObject
);
const { results, isLoading } = useCollectionData( {
queryPrices: true,
queryState,
queryState: {
...backendQueryState,
...queryState,
},
isEditor,
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe( 'usePriceConstraints', () => {
);
const container = renderer.root.findByType( 'div' );

expect( container.props[ 'data-maxPriceConstraint' ] ).toBe( 2000 );
expect( container.props[ 'data-maxPriceConstraint' ] ).toBe( 1100 );

renderer.update( <TestComponent price={ 1999 } /> );

Expand All @@ -80,10 +80,10 @@ describe( 'usePriceConstraints', () => {
const renderer = TestRenderer.create( <TestComponent price={ 999 } /> );
const container = renderer.root.findByType( 'div' );

expect( container.props[ 'data-minPriceConstraint' ] ).toBe( 0 );
expect( container.props[ 'data-minPriceConstraint' ] ).toBe( 900 );

renderer.update( <TestComponent price={ 1999 } /> );

expect( container.props[ 'data-minPriceConstraint' ] ).toBe( 1000 );
expect( container.props[ 'data-minPriceConstraint' ] ).toBe( 1900 );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const usePriceConstraint = (
minorUnit: number,
direction: string
) => {
const step = 10 * 10 ** minorUnit;
const step = 1 * 10 ** minorUnit;
let currentConstraint = null;
const parsedPrice = parseFloat( price );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix: show correct attribute and price filter data on the category/tag template.
13 changes: 13 additions & 0 deletions plugins/woocommerce/src/Blocks/BlockTypes/AttributeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ class AttributeFilter extends AbstractBlock {
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'attributes', array_values( wc_get_attribute_taxonomies() ) );

// Enqueue any `queryState` that the UI will need to be aware of
// (Ex: the category id if we're on a category page, the tag id if we're on a tag page/etc).
$query_state = [];

if ( is_product_category() ) {
$query_state['category'] = get_queried_object_id();
}
if ( is_product_tag() ) {
$query_state['tag'] = get_queried_object()->term_id;
}

$this->asset_data_registry->add( 'queryState', $query_state );
}

/**
Expand Down
26 changes: 26 additions & 0 deletions plugins/woocommerce/src/Blocks/BlockTypes/PriceFilter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Automattic\WooCommerce\Blocks\BlockTypes;

/**
Expand All @@ -14,4 +15,29 @@ class PriceFilter extends AbstractBlock {
protected $block_name = 'price-filter';
const MIN_PRICE_QUERY_VAR = 'min_price';
const MAX_PRICE_QUERY_VAR = 'max_price';

/**
* Extra data passed through from server to client for block.
*
* @param array $attributes Any attributes that currently are available from the block.
* Note, this will be empty in the editor context when the block is
* not in the post content on editor load.
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'attributes', array_values( wc_get_attribute_taxonomies() ) );

// Enqueue any `queryState` that the UI will need to be aware of
// (Ex: the category id if we're on a category page, the tag id if we're on a tag page/etc).
$query_state = [];

if ( is_product_category() ) {
$query_state['category'] = get_queried_object_id();
}
if ( is_product_tag() ) {
$query_state['tag'] = get_queried_object()->term_id;
}

$this->asset_data_registry->add( 'queryState', $query_state );
}
}

0 comments on commit 73e7d57

Please sign in to comment.