Skip to content

Instantly share code, notes, and snippets.

@christianschoenmakers
Created September 1, 2018 08:42
Show Gist options
  • Save christianschoenmakers/2faf254c49302472b0ce6069eb175813 to your computer and use it in GitHub Desktop.
Save christianschoenmakers/2faf254c49302472b0ce6069eb175813 to your computer and use it in GitHub Desktop.
FacetWP - map taxonomies of parent product to variation
add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
$extra_taxonomies = array(
'producttype' => 'product-type', // Facet WP facet name => taxonomy name
);
// Check if facet name is extra taxonomy
if ( array_key_exists($params['facet']['name'], $extra_taxonomies) ){
// Set taxonomy to check
$taxonomy_to_check = $extra_taxonomies[$params['facet']['name']];
// Check if post is product variation and has a parent
$parent_id = wp_get_post_parent_id($params['defaults']['post_id']);
if(get_post_type($params['defaults']['post_id']) == 'product_variation' && $parent_id){
// Create new data row
$data_row = $params['defaults'];
// Get data from taxonomy
$parent_post_terms = wp_get_post_terms( $parent_id, $taxonomy_to_check);
foreach($parent_post_terms AS $taxonomy_term){
$data_row['facet_value'] = $taxonomy_term->slug;
$data_row['facet_display_value'] = $taxonomy_term->name;
$data_row['term_id'] = $taxonomy_term->term_id;
$data_row['parent_id'] = $taxonomy_term->parent;
// Append to rows
$rows[] = $data_row;
}
}
}
return $rows;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment