<?php
// See: https://facetwp.com/help-center/developers/hooks/advanced-hooks/facetwp_facet_sources/#add-a-non-existing-custom-field-option-and-handle-its-indexing
// Add the new data source
add_filter( 'facetwp_facet_sources', function( $sources, $context ) {
if ( 'default' == $context ) { // Only in facets' Data source dropdowns, not in Listing Builder item dropdowns
$sources['woocommerce']['choices']['woo/price_incl_tax'] = __( 'Price (incl. tax)' );
}
return $sources;
}, 20, 2 );
// Handle the new data source
add_filter( 'facetwp_indexer_post_facet', function( $return, $params ) {
$facet = $params['facet'];
$defaults = $params['defaults'];
$post_id = (int) $defaults['post_id'];
if ( 'product' == get_post_type( $post_id ) && 'woo/price_incl_tax' == $defaults['facet_source'] ) {
$product = wc_get_product( $post_id );
$price = wc_get_price_including_tax( $product );
$defaults['facet_value'] = $price;
$defaults['facet_display_value'] = $price;
FWP()->indexer->index_row( $defaults );
return true;
}
return $return;
}, 10, 2 );