Skip to content

Instantly share code, notes, and snippets.

@scottnelle
Last active September 13, 2019 11:32
Show Gist options
  • Save scottnelle/ab223beff8f6fa5101a0 to your computer and use it in GitHub Desktop.
Save scottnelle/ab223beff8f6fa5101a0 to your computer and use it in GitHub Desktop.
Exclude specific categories from being considered when WooCommerce selects related products. $exclude is an array of category names or slugs. Note that WooCommerce caches products selected by category for a day, so once you install this code you'll have to save a product to clear the cache and see the results.
<?php
/**
* Exclude specific categories from being considered when selecting related products
*/
function my_exclude_related_terms( $terms ) {
$exclude = array( 'First Category', 'second-category' );
foreach ( $terms as $i => $term ) {
if ( in_array( $term->name, $exclude ) || in_array( $term->slug, $exclude ) ) {
unset( $terms[ $i ] );
}
}
return $terms;
}
add_filter( 'woocommerce_get_related_category_terms', 'my_exclude_related_terms' );
@pansotdev
Copy link

Work with WooCommerce Version 3+?

@davegreenwp
Copy link

@pansotdev The filter is now woocommerce_get_related_product_cat_terms

@tristadeleon
Copy link

Where do I put this?

@makeonlineshop
Copy link

hello, sorry but i tried this code and it does not work for me, any idea ?
No error in log.
Thank you for your help.

@GonzoFX
Copy link

GonzoFX commented Sep 13, 2019

Doesn't work...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment