FacetWP – index variation attribute with “Any” select

mgibbs189

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

add_filter( ‘facetwp_indexer_row_data’, function( $rows, $params ) {
$defaults = $params[‘defaults’];
$post_id = (int) $defaults[‘post_id’];

if ( 0 === strpos( $defaults[‘facet_source’], ‘cf/attribute_pa_’ ) ) {
if ( ‘product_variation’ == get_post_type( $post_id ) ) {
$parent_id = wp_get_post_parent_id( $post_id );
$tax = str_replace( ‘cf/attribute_’, ”, $defaults[‘facet_source’] );
$terms = get_the_terms( $parent_id, $tax );

if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$temp = $defaults;
$temp[‘term_id’] = $term->term_id;
$temp[‘facet_value’] = $term->slug;
$temp[‘facet_display_value’] = $term->name;
$temp[‘variation_id’] = $post_id;
$temp[‘post_id’] = $parent_id;
$rows[] = $temp;
}
}
}
}

return $rows;
}, 999, 2 );

{{/gist}}