Skip to content

Instantly share code, notes, and snippets.

@digamber89
Created April 4, 2022 08:12
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 digamber89/f877d8fd4597909580dcbf9456ce48ff to your computer and use it in GitHub Desktop.
Save digamber89/f877d8fd4597909580dcbf9456ce48ff to your computer and use it in GitHub Desktop.
Skip a certain category from being indexed as a facet
<?php
function cm_tsfwc_20220404_skip_product_category( $formatted_data, $raw_data, $object_id ) {
$categories = get_the_terms( $raw_data->get_id(), 'product_cat' );
$category_names = [];
if ( ! empty( $categories ) ) {
foreach ( $categories as $category ) {
$parents_ids = get_ancestors( $category->term_id, 'product_cat' );
array_unshift( $parents_ids, $category->term_id );
foreach ( $parents_ids as $parent_id ) {
//replace 29 with the id of product category you do not want to index
if($parent_id == '29'){
continue;
}
$term = get_term_by( 'id', $parent_id, 'product_cat', 'taxonomy' );
$category_names[] = html_entity_decode( $term->name );
}
}
}
if ( ! empty( $category_names ) ) {
$formatted_data['category'] = is_array( $category_names ) && ! empty( $category_names ) ? array_values( array_unique( $category_names ) ) : [];
}
return $formatted_data;
}
add_filter( 'cm_tsfwc_data_before_entry', 'cm_tsfwc_20220404_skip_product_category', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment