<?php
/** get_price for woocommerce grouped products **/
add_filter( 'facetwp_indexer_post_facet', function( $bypass, $params ) {
$defaults = $params['defaults'];
$post_id = (int) $defaults['post_id'];
$post_type = get_post_type( $post_id );
if ( $bypass || 'product' != $post_type ) {
// skip if $bypass is already true
// skip if not product post type
return $bypass;
}
$product = wc_get_product( $post_id );
if ( ! $product || 'grouped' != $product->get_type() ) {
// skip if not product not found
// skip if not grouped product
return $bypass; // skip
}
if ( 'price' == $params['facet']['name'] ) { // change 'price' for name of your facet, this is for use with Woocommerce -> Price selected as the data source in the facet settings
$child_prices = array();
$children = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' );
foreach ( $children as $child ) {
if ( '' !== $child->get_price() ) {
$child_prices[] = $child->get_price();
}
}
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
$max_price = max( $child_prices );
} else {
$min_price = '';
$max_price = '';
}
if ( '' != $min_price ) {
$defaults['facet_value'] = $min_price;
$defaults['facet_display_value'] = $max_price;
FWP()->indexer->index_row( $defaults );
return true;
}
}
return $bypass;
}, 10, 3 );