Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active July 29, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/ed2e2a25009ba74e5944cbf813db1fc5 to your computer and use it in GitHub Desktop.
Save mgibbs189/ed2e2a25009ba74e5944cbf813db1fc5 to your computer and use it in GitHub Desktop.
FacetWP - add data source for WooCommerce featured products
<?php
/**
* Add the new "Featured" Data Source within the WooCommerce heading
*/
add_filter( 'facetwp_facet_sources', function( $sources ) {
$sources['woocommerce']['choices']['woo/featured'] = __( 'Featured' );
return $sources;
}, 20 );
/**
* Index the featured products
* Note: set the facet type to "Checkboxes" and the data source to "Featured"
* then save and re-index
*/
add_filter( 'facetwp_indexer_post_facet', function( $return, $params ) {
$facet = $params['facet'];
$defaults = $params['defaults'];
$source = isset( $facet['source'] ) ? $facet['source'] : '';
if ( 'woo/featured' == $source ) {
$current_id = (int) $defaults['post_id'];
$all_featured_ids = wc_get_featured_product_ids();
if ( in_array( $current_id, $all_featured_ids ) ) {
$defaults['facet_value'] = 1;
$defaults['facet_display_value'] = __( 'Is Featured' );
FWP()->indexer->index_row( $defaults );
}
$return = true;
}
return $return;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment