Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ensure global categories include products in child categories
<?php
/**
* Ensure global categories include products in child categories
*/
function prefix_add_parents_to_permitted_cats( $categories, $product_id, $global, $rule ) {
// Iterate through each category
// If it's got children, ensure they're added to the list
if( $categories ) {
foreach( $categories as $cat_id ) {
$children = get_term_children( $cat_id, 'product_cat' );
if( $children ) {
$categories = array_merge( $categories, $children );
}
}
}
return $categories;
}
add_filter( 'pewc_filter_permitted_cats', 'prefix_add_parents_to_permitted_cats', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment