- Help Center Home
- Getting started
- Introducing FacetWP
- Installation and updates
- FAQ
- How it works
- What are facets?
- Facet types
- Indexing
- Listing templates
- Extras & integrations
- Add-on features and extras
- Using FacetWP with …
- Built-in integrations
- Advanced Custom Fields
- WooCommerce
- SearchWP
- WP-CLI
- Add-on integrations
- Bricks
- Elementor
- Beaver Builder
- WP Recipe Maker and Tasty Recipes
- Relevanssi
- WPML and Polylang
- Pods
- Meta Box
- Flatsome (theme)
- External integrations
- Listify (theme)
- Listable (theme)
- WPGraphQL
- Document Library Pro
- Tips & tricks
- WooCommerce plugins
- WordPress multi-site
- WP All Import
- WebToffee Import Export
- WP Job Manager
- Easy Digital Downloads
- EDD Reviews
- Intuitive Custom Post Order
- Custom Taxonomy Order
- Post Types Order
- Genesis framework
- WP External Links
- ElasticPress
- Caching & hosting
- WP Rocket
- Cloudflare
- New Relic
- WP Engine
- Fast Velocity Minify
- Incompatibilities
- Incompatible plugins
- Troubleshooting
- Troubleshooting guide
- Using the right query
- Common issues
- Get support
- Developers
- Hooks reference
- JavaScript reference
- Shortcodes reference
- FacetWP REST API
- How FacetWP works
- The FacetWP URL
- FacetWP speed and limits
- Tutorials
- Code snippets
- Changelog
How to Add a “Percent Discount” Facet
To show “% discount” in a facet, we’ll first create the facet:

We’re setting the Data source to “Post Type” just as a placeholder.
Next, we add an indexer hook to calculates the percent discount and modify the index value. Add the following into your (child) theme’s functions.php
:
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'percent_discount' == $params['facet_name'] ) {
$post_id = (int) $params['post_id'];
if ( 'product' == get_post_type( $post_id ) ) {
$product = wc_get_product( $post_id );
$discount = 0;
if ( $product ) {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$discount = round( 100 * ( $regular_price - $sale_price ) / $regular_price );
}
$params['facet_value'] = $discount;
$params['facet_display_value'] = $discount;
}
}
return $params;
}, 10, 2 );
Make sure to re-index afterwards.