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

Make Filter Products by Stock block compatible with PHP rendered Classic Template block #6261

Merged
merged 19 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
09efe3f
Enable Attribute Filter block to work with the PHP rendered Classic T…
tjcafferkey Apr 6, 2022
ead2195
Merge branch 'trunk' into update/filter-product-by-attribute-php-temp…
tjcafferkey Apr 7, 2022
859df13
Check for presence of option before rendering it
tjcafferkey Apr 7, 2022
8ad3ea2
Merge branch 'trunk' of github.com:woocommerce/woocommerce-gutenberg-…
gigitux Apr 11, 2022
b468320
improve filter product by attribute
gigitux Apr 11, 2022
829da22
Merge branch 'trunk' of github.com:woocommerce/woocommerce-gutenberg-…
gigitux Apr 11, 2022
89a173d
fix pagination problem
gigitux Apr 11, 2022
0bdf8a7
fix check when two filter block with same attribute are used
gigitux Apr 12, 2022
5beac29
Merge branch 'trunk' into update/filter-product-by-attribute-php-temp…
gigitux Apr 12, 2022
5f14396
Merge branch 'update/filter-product-by-attribute-php-template' of git…
gigitux Apr 13, 2022
eaccd7e
Merge branch 'trunk' of github.com:woocommerce/woocommerce-gutenberg-…
gigitux Apr 14, 2022
92c8b5d
fix filter by stock for PHP templates
gigitux Apr 14, 2022
aa0da23
fix naming and comment
gigitux Apr 14, 2022
38e3642
Update filter key in ClassicTemplate
tjcafferkey Apr 19, 2022
03c607f
Update stock filter block for PHP templates when the filter button is…
tjcafferkey Apr 20, 2022
5f4de0c
Remove unused useEffect and fix ESLint error
tjcafferkey Apr 20, 2022
d37fb36
Set active stock filter and track using local state
tjcafferkey Apr 20, 2022
3ee7eb8
ESLint fix
tjcafferkey Apr 20, 2022
8cc2da4
Remove unncessary dependencies from useEffect
tjcafferkey Apr 20, 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
18 changes: 17 additions & 1 deletion assets/js/blocks/attribute-filter/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import classNames from 'classnames';
import { getSettingWithCoercion } from '@woocommerce/settings';
import { getQueryArgs, removeQueryArgs } from '@wordpress/url';
import { isBoolean, isString } from '@woocommerce/types';
import {
PREFIX_QUERY_ARG_FILTER_TYPE,
PREFIX_QUERY_ARG_QUERY_TYPE,
} from '@woocommerce/utils';

/**
* Internal dependencies
Expand All @@ -36,6 +40,7 @@ import {
getActiveFilters,
areAllFiltersRemoved,
isQueryArgsEqual,
parseTaxonomyToGenerateURL,
} from './utils';

/**
Expand Down Expand Up @@ -392,9 +397,20 @@ const AttributeFilterBlock = ( {
getQueryArgs( window.location.href )
);

const parsedTaxonomy = parseTaxonomyToGenerateURL(
attributeObject?.taxonomy
);

const url = currentQueryArgKeys.reduce(
( currentUrl, queryArg ) =>
removeQueryArgs( currentUrl, queryArg ),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this logic because with the previous implementation, ALL query parameters were removed, instead only the query parameters related to the attribute filter have to be removed.

queryArg.includes(
PREFIX_QUERY_ARG_QUERY_TYPE + parsedTaxonomy
) ||
queryArg.includes(
PREFIX_QUERY_ARG_FILTER_TYPE + parsedTaxonomy
)
? removeQueryArgs( currentUrl, queryArg )
: currentUrl,
window.location.href
);

Expand Down
19 changes: 11 additions & 8 deletions assets/js/blocks/attribute-filter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@
*/
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
import { QueryArgs } from '@wordpress/url/build-types/get-query-args';

/**
* Internal dependencies
*/
import { getUrlParameter } from '../../utils/filters';
import {
getUrlParameter,
PREFIX_QUERY_ARG_FILTER_TYPE,
PREFIX_QUERY_ARG_QUERY_TYPE,
} from '@woocommerce/utils';

interface Param {
attribute: string;
operator: string;
slug: Array< string >;
}

export const parseTaxonomyToGenerateURL = ( taxonomy: string ) =>
taxonomy.replace( 'pa_', '' );

export const formatParams = ( url: string, params: Array< Param > = [] ) => {
const paramObject: Record< string, string > = {};

params.forEach( ( param ) => {
const { attribute, slug, operator } = param;

// Custom filters are prefix with `pa_` so we need to remove this.
const name = attribute.replace( 'pa_', '' );
const name = parseTaxonomyToGenerateURL( attribute );
const values = slug.join( ',' );
const queryType = `query_type_${ name }`;
const queryType = `${ PREFIX_QUERY_ARG_QUERY_TYPE }${ name }`;
const type = operator === 'in' ? 'or' : 'and';

// The URL parameter requires the prefix filter_ with the attribute name.
paramObject[ `filter_${ name }` ] = values;
paramObject[ `${ PREFIX_QUERY_ARG_FILTER_TYPE }${ name }` ] = values;
paramObject[ queryType ] = type;
} );

Expand Down
97 changes: 94 additions & 3 deletions assets/js/blocks/stock-filter/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ import {
useQueryStateByContext,
useCollectionData,
} from '@woocommerce/base-context/hooks';
import { getSetting } from '@woocommerce/settings';
import { getSetting, getSettingWithCoercion } from '@woocommerce/settings';
import { useCallback, useEffect, useState, useMemo } from '@wordpress/element';
import CheckboxList from '@woocommerce/base-components/checkbox-list';
import FilterSubmitButton from '@woocommerce/base-components/filter-submit-button';
import Label from '@woocommerce/base-components/filter-element-label';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { decodeEntities } from '@wordpress/html-entities';
import { isBoolean } from '@woocommerce/types';
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
import { PREFIX_QUERY_ARG_FILTER_TYPE } from '@woocommerce/utils';

/**
* Internal dependencies
*/
import { previewOptions } from './preview';
import './style.scss';
import { getActiveFilters } from './utils';

export const QUERY_PARAM_KEY = PREFIX_QUERY_ARG_FILTER_TYPE + 'stock_status';

/**
* Component displaying an stock status filter.
Expand All @@ -34,6 +40,16 @@ const StockStatusFilterBlock = ( {
attributes: blockAttributes,
isEditor = false,
} ) => {
const filteringForPhpTemplate = getSettingWithCoercion(
'is_rendering_php_template',
false,
isBoolean
);

const [ hasSetPhpFilterDefaults, setHasSetPhpFilterDefaults ] = useState(
false
);

const [ hideOutOfStockItems ] = useState(
getSetting( 'hideOutOfStockItems', false )
);
Expand All @@ -46,7 +62,9 @@ const StockStatusFilterBlock = ( {
: { outofstock, ...otherStockStatusOptions }
);

const [ checked, setChecked ] = useState( [] );
const [ checked, setChecked ] = useState(
getActiveFilters( STOCK_STATUS_OPTIONS, QUERY_PARAM_KEY )
);
const [ displayedOptions, setDisplayedOptions ] = useState(
blockAttributes.isPreview ? previewOptions : []
);
Expand Down Expand Up @@ -148,6 +166,33 @@ const StockStatusFilterBlock = ( {
initialOptions,
] );

/**
* Used to redirect the page when filters are changed so templates using the Classic Template block can filter.
*
* @param {Array} checkedOptions Array of checked stock options.
*/
const redirectPageForPhpTemplate = ( checkedOptions ) => {
if ( checkedOptions.length === 0 ) {
const url = removeQueryArgs(
window.location.href,
QUERY_PARAM_KEY
);

if ( url !== window.location.href ) {
window.location.href = url;
}
return;
}

const newUrl = addQueryArgs( window.location.href, {
[ QUERY_PARAM_KEY ]: checkedOptions.join( ',' ),
} );

if ( newUrl !== window.location.href ) {
window.location.href = newUrl;
}
};

const onSubmit = useCallback(
( isChecked ) => {
if ( isEditor ) {
Expand All @@ -156,8 +201,17 @@ const StockStatusFilterBlock = ( {
if ( isChecked ) {
setProductStockStatusQuery( checked );
}
// For PHP templates when the filter button is enabled.
if ( filteringForPhpTemplate ) {
redirectPageForPhpTemplate( checked );
}
},
[ isEditor, setProductStockStatusQuery, checked ]
[
isEditor,
setProductStockStatusQuery,
checked,
filteringForPhpTemplate,
]
);

// Track checked STATE changes - if state changes, update the query.
Expand All @@ -183,6 +237,43 @@ const StockStatusFilterBlock = ( {
}
}, [ checked, currentCheckedQuery, previousCheckedQuery ] );

/**
* Important: For PHP rendered block templates only.
*/
useEffect( () => {
if ( filteringForPhpTemplate ) {
setChecked( checked );
// Only automatically redirect if the filter button is not active.
if ( ! blockAttributes.showFilterButton ) {
redirectPageForPhpTemplate( checked );
}
}
}, [
filteringForPhpTemplate,
productStockStatusQuery,
checked,
blockAttributes.queryType,
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
blockAttributes.showFilterButton,
] );

/**
* Important: For PHP rendered block templates only.
*/
useEffect( () => {
if ( ! hasSetPhpFilterDefaults && filteringForPhpTemplate ) {
setProductStockStatusQuery(
getActiveFilters( STOCK_STATUS_OPTIONS, QUERY_PARAM_KEY )
);
setHasSetPhpFilterDefaults( true );
}
}, [
STOCK_STATUS_OPTIONS,
filteringForPhpTemplate,
setProductStockStatusQuery,
hasSetPhpFilterDefaults,
setHasSetPhpFilterDefaults,
] );

/**
* When a checkbox in the list changes, update state.
*/
Expand Down
22 changes: 22 additions & 0 deletions assets/js/blocks/stock-filter/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import { isString } from '@woocommerce/types';
import { getUrlParameter } from '@woocommerce/utils';

export const getActiveFilters = (
filters: Record< string, string >,
queryParamKey: 'filter_stock_status'
) => {
const params = getUrlParameter( queryParamKey );

if ( ! params ) {
return [];
}

const parsedParams = isString( params ) ? params.split( ',' ) : params;

return Object.keys( filters ).filter( ( filter ) =>
parsedParams.includes( filter )
);
};
4 changes: 4 additions & 0 deletions assets/js/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { getQueryArg } from '@wordpress/url';
*
* @param {string} name Parameter you want the value of.
*/

export const PREFIX_QUERY_ARG_QUERY_TYPE = 'query_type_';
export const PREFIX_QUERY_ARG_FILTER_TYPE = 'filter_';

export function getUrlParameter( name: string ) {
if ( ! window ) {
return null;
Expand Down
8 changes: 8 additions & 0 deletions assets/js/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './attributes-query';
export * from './attributes';
export * from './filters';
export * from './global-style';
export * from './notices';
export * from './products';
export * from './shared-attributes';
export * from './useThrottle';
1 change: 1 addition & 0 deletions bin/webpack-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const getAlias = ( options = {} ) => {
`../assets/js/${ pathPart }previews/`
),
'@woocommerce/types': path.resolve( __dirname, `../assets/js/types/` ),
'@woocommerce/utils': path.resolve( __dirname, `../assets/js/utils/` ),
};
};

Expand Down
52 changes: 52 additions & 0 deletions src/BlockTypes/ClassicTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ class ClassicTemplate extends AbstractDynamicBlock {
*/
protected $api_version = '2';

const FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM = 'filter_stock_status';

/**
* Initialize this block.
*/
protected function initialize() {
parent::initialize();
add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 );
add_filter( 'query_vars', array( $this, 'add_query_vars_filter' ) );
add_filter( 'woocommerce_product_query_meta_query', array( $this, 'filter_products_by_stock' ), 10, 2 );

}

/**
Expand Down Expand Up @@ -255,4 +260,51 @@ public function add_alignment_class_to_wrapper( string $content, array $block )
}


/**
* Filter products by stock status when as query param there is "filter_stock_status"
*
* @param array $meta_query Meta query.
* @return array
*/
public function filter_products_by_stock( $meta_query ) {
if ( is_admin() ) {
return $meta_query;
}

$stock_status = array_keys( wc_get_product_stock_status_options() );
$values = get_query_var( self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM );

$values_to_array = explode( ',', $values );

$filtered_values = array_filter(
$values_to_array,
function( $value ) use ( $stock_status ) {
return in_array( $value, $stock_status, true );
}
);

if ( ! empty( $filtered_values ) ) {

$meta_query[] = array(
'key' => '_stock_status',
'value' => $filtered_values,
'compare' => 'IN',
);
}
return $meta_query;
}


/**
* Add custom query params
*
* @param array $vars Query vars.
* @return array Query vars.
*/
public function add_query_vars_filter( $vars ) {
$vars[] = self::FILTER_PRODUCTS_BY_STOCK_QUERY_PARAM;
return $vars;
}


}
3 changes: 2 additions & 1 deletion tests/js/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"@woocommerce/shared-context": "assets/js/shared/context",
"@woocommerce/shared-hocs": "assets/js/shared/hocs",
"@woocommerce/blocks-test-utils": "tests/utils",
"@woocommerce/types": "assets/js/types"
"@woocommerce/types": "assets/js/types",
"@woocommerce/utils": "assets/js/utils"
},
"setupFiles": [
"@wordpress/jest-preset-default/scripts/setup-globals.js",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"@woocommerce/shared-hocs": [ "assets/js/shared/hocs" ],
"@woocommerce/type-defs/*": [ "assets/js/types/type-defs/*" ],
"@woocommerce/types": [ "assets/js/types" ],
"@woocommerce/storybook-controls": [ "storybook/custom-controls" ]
"@woocommerce/storybook-controls": [ "storybook/custom-controls" ],
"@woocommerce/utils": [ "assets/js/utils" ]
}
}
}