Last active
September 13, 2019 11:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 The filter is now woocommerce_get_related_product_cat_terms
Where do I put this?
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.
Doesn't work...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Work with WooCommerce Version 3+?