Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save plugin-republic/c4906e99c64ec4a2b9ae17525824cc50 to your computer and use it in GitHub Desktop.
Save plugin-republic/c4906e99c64ec4a2b9ae17525824cc50 to your computer and use it in GitHub Desktop.
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