1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Global Add-Ons
  5. Ensure global categories include products in child categories
  1. Home
  2. Knowledge Base
  3. WooCommerce Product Add-Ons Ultimate
  4. Ensure global categories include products in child categories

Ensure global categories include products in child categories

You can ensure that any categories you select in global groups that use the ‘Categories’ rule will include child products. Just add this snippet:

<?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 );

This is how to add a snippet.

Was this article helpful?

Related Articles