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

Make Filters Products by Price work with Active Filters block #6245

Merged
merged 4 commits into from
Apr 21, 2022
Merged
Changes from 2 commits
Commits
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
51 changes: 46 additions & 5 deletions assets/js/blocks/price-filter/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ function formatParams( url, params ) {
return addQueryArgs( cleanUrl, paramObject );
}

/**
* Formats price values taking into account precision
*
* @param {string} value
* @param {number} minorUnit
*
* @return {number} Formatted price.
*/

function formatPrice( value, minorUnit ) {
return Number( value ) * 10 ** minorUnit;
}

/**
* Component displaying a price filter.
*
Expand All @@ -71,6 +84,8 @@ const PriceFilterBlock = ( { attributes, isEditor = false } ) => {
''
);

const [ hasSetPriceDefault, setHasSetPriceDefault ] = useState( false );
albarin marked this conversation as resolved.
Show resolved Hide resolved

const [ queryState ] = useQueryStateByContext();
const { results, isLoading } = useCollectionData( {
queryPrices: true,
Expand All @@ -84,18 +99,18 @@ const PriceFilterBlock = ( { attributes, isEditor = false } ) => {

const [ minPriceQuery, setMinPriceQuery ] = useQueryStateByKey(
'min_price',
Number( minPriceParam ) * 10 ** currency.minorUnit || null
formatPrice( minPriceParam, currency.minorUnit ) || null
);
const [ maxPriceQuery, setMaxPriceQuery ] = useQueryStateByKey(
'max_price',
Number( maxPriceParam ) * 10 ** currency.minorUnit || null
formatPrice( maxPriceParam, currency.minorUnit ) || null
);

const [ minPrice, setMinPrice ] = useState(
Number( minPriceParam ) * 10 ** currency.minorUnit || null
formatPrice( minPriceParam, currency.minorUnit ) || null
);
const [ maxPrice, setMaxPrice ] = useState(
Number( maxPriceParam ) * 10 ** currency.minorUnit || null
formatPrice( maxPriceParam, currency.minorUnit ) || null
);

const { minConstraint, maxConstraint } = usePriceConstraints( {
Expand All @@ -108,6 +123,33 @@ const PriceFilterBlock = ( { attributes, isEditor = false } ) => {
minorUnit: currency.minorUnit,
} );

/**
* Important: For PHP rendered block templates only.
*
* When we render the PHP block template (e.g. Classic Block) we need
* to set the default min_price and max_price values from the URL
*/
albarin marked this conversation as resolved.
Show resolved Hide resolved
useEffect( () => {
if ( ! hasSetPriceDefault && filteringForPhpTemplate ) {
setMinPriceQuery(
formatPrice( minPriceParam, currency.minorUnit )
);
setMaxPriceQuery(
formatPrice( maxPriceParam, currency.minorUnit )
);

setHasSetPriceDefault( true );
}
}, [
currency.minorUnit,
filteringForPhpTemplate,
hasSetPriceDefault,
maxPriceParam,
minPriceParam,
setMaxPriceQuery,
setMinPriceQuery,
] );

// Updates the query based on slider values.
const onSubmit = useCallback(
( newMinPrice, newMaxPrice ) => {
Expand All @@ -126,7 +168,6 @@ const PriceFilterBlock = ( { attributes, isEditor = false } ) => {
min_price: finalMinPrice / 10 ** currency.minorUnit,
max_price: finalMaxPrice / 10 ** currency.minorUnit,
} );

// If the params have changed, lets reload the page.
if ( window.location.href !== newUrl ) {
window.location.href = newUrl;
Expand Down