facetwp index “any” for woocommerce variations

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php

/** handles indexing when a woocommerce variation has “any” selected instead of specific values **/
add_filter( ‘facetwp_indexer_row_data’, function( $rows, $args ) {

if ( 0 === strpos( $args[‘defaults’][‘facet_source’], ‘cf/attribute_pa_’ ) ) {
foreach ( $rows AS $row ) {
if ( ‘product_variation’ == get_post_type( $row[‘post_id’] ) && ” == $row[‘facet_value’] ) {
$parent = wp_get_post_parent_id( $row[‘post_id’] );
if ( 0 === strpos( $row[‘facet_source’], ‘cf/attribute_pa_’ ) ) {
$taxonomy = str_replace( ‘cf/attribute_’, ”, $row[‘facet_source’] );
$terms = get_the_terms( $parent, $taxonomy );
if ( !is_wp_error( $terms ) && !empty( $terms ) ) {
foreach ( $terms AS $term ) {
$params = $row;
$params[‘term_id’] = $term->term_id;
$params[‘facet_display_value’] = $term->name;
$params[‘facet_value’] = $term->slug;
$params[‘variation_id’] = $row[‘post_id’];
$params[‘post_id’] = $parent;
$rows[] = $params;
}
}
}
}
}
}

return $rows;

}, 11,2 );
{{/gist}}