Created
March 7, 2019 16:46
-
-
Save djrmom/73394e2e94569b9b14ca4c5ff172c3e3 to your computer and use it in GitHub Desktop.
facetwp index "any" for woocommerce variations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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