Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created March 7, 2019 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djrmom/73394e2e94569b9b14ca4c5ff172c3e3 to your computer and use it in GitHub Desktop.
Save djrmom/73394e2e94569b9b14ca4c5ff172c3e3 to your computer and use it in GitHub Desktop.
facetwp index "any" for woocommerce variations
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment