FacetWP – add a WooCommerce “On Sale” facet

mgibbs189

{{gist file=”test.php” lang=”php”}}
<?php

// Create a facet named “on_sale” with the Data Source of “Post Type”
// Add this to functions.php
function fwp_is_sale( $params, $class ) {
if ( ‘is_sale’ == $params[‘facet_name’] ) {
$post_id = (int) $params[‘post_id’];
$variable_sale_price = get_post_meta( $post_id, ‘_min_variation_sale_price’, true );
$sale_price = get_post_meta( $post_id, ‘_sale_price’, true );

if ( ! empty( $variable_sale_price ) || ! empty( $sale_price ) ) {
$params[‘facet_value’] = 1;
$params[‘facet_display_value’] = ‘On sale’;
return $params;
}

// If not on sale, skip the facet for this post
return false;
}

// Proceed normally
return $params;
}
add_filter( ‘facetwp_index_row’, ‘fwp_is_sale’, 10, 2 );
{{/gist}}