FacetWP – index custom field attached to product variation

mgibbs189

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

// Force indexing of product variations
function fwp_index_product_variations( $args ) {
$post_types = get_post_types();
$post_types[] = ‘product_variation’;
$args[‘post_type’] = $post_types;
return $args;
}
add_filter( ‘facetwp_indexer_query_args’, ‘fwp_index_product_variations’ );

// Index color field for the parent product
function fwp_index_color( $params, $class ) {
if ( ‘cf/_ax_color_field’ == $params[‘facet_source’] ) {
$variation = get_post( $params[‘post_id’] );
$parent_product_id = (int) $variation->post_parent;
$params[‘post_id’] = $parent_product_id;
}
return $params;
}
add_filter( ‘facetwp_index_row’, ‘fwp_index_color’, 10, 2 );
{{/gist}}