Skip to content

Instantly share code, notes, and snippets.

@pablogiralt
Created September 4, 2018 20:20
Show Gist options
  • Save pablogiralt/fd9e145318e0e11d338c1ee0dc4a43bb to your computer and use it in GitHub Desktop.
Save pablogiralt/fd9e145318e0e11d338c1ee0dc4a43bb to your computer and use it in GitHub Desktop.
Price range filter for Woocommerce with facetwp
/*
* 1. Create the filter by price Facet:
* - Set "Facet type" to "Checkboxes"
* - Set "Data source" to "Price"
* - Set "Sort by" to "Raw Value"
* 2. Set $price_facet_name to the name of the crewated facet
*/
function index_custom_price_range( $params, $class ) {
$price_facet_name = 'price';
if ( $price_facet_name == $params['facet_name'] ) {
$price = (double) $params['facet_value'];
if( $price > 0 ){
$symbol = get_woocommerce_currency_symbol();
if ( $price < 25 ) {
$params['facet_value'] = 1;
$params['facet_display_value'] = sprintf ( __('Under %s', 'woocommerce'), $symbol.'25' );
}
elseif ( $price < 50 ) {
$params['facet_value'] = 2;
$params['facet_display_value'] = $symbol.'25-'.$symbol.'50';
}
elseif ( $price < 100 ) {
$params['facet_value'] = 3;
$params['facet_display_value'] = $symbol.'50-'.$symbol.'100';
}
elseif ( $price < 200 ) {
$params['facet_value'] = 4;
$params['facet_display_value'] = $symbol.'100-'.$symbol.'200';
}
else {
$params['facet_value'] = 5;
$params['facet_display_value'] = sprintf ( __('Above %s', 'woocommerce'), $symbol.'200' );
}
}
}
return $params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment