December 22, 2021
Handy if you’re using the default category as “All Products” and want to prevent unrelated items from appearing related when no other categories are selected.
// exclude default category from related products
function exclude_default_from_related( $categories ){
// array of category id's that should be excluded
$exclude_cats = array( 15 );
foreach( $categories as $index => $cat ){
if( in_array( $cat, $exclude_cats ) ){
unset( $categories[ $index ] );
}
}
return $categories;
}
add_filter( 'woocommerce_get_related_product_cat_terms', 'exclude_default_from_related' );