Omit certain categories using a hook
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page pleaseIf you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.
Quote from Byron on May 29, 2024, 11:47Hi,
My client wants to omit some of the categories on one of their product archives.
eg. Product category page 1 they only want to show Tables, Chairs.
All other product categories will show the full catalogue, Tables, Chairs, Cushions, Benches, Sets, etc. etc.I've got the conditional statement in my template so can use two separate shortcodes, but it seems that I can't just select certain categories to show in a shortcode. Is this correct?
As a test I've tried to add a filter using the hook 'woof_get_tax_query' to omit a category ID 169 by adding this to my functions.php - It has no effect.
add_filter('woof_get_tax_query', 'exclude_product_category_from_woof', 10, 1);
function exclude_product_category_from_woof($tax_query) {
foreach ($tax_query as $key => $query) {
if ($query['taxonomy'] === 'product_cat') {
$tax_query[$key]['terms'] = array_diff($query['terms'], [169]);
}
}
return $tax_query;
}Am I in the correct ballpark? :)
Many thanks
Byron
Hi,
My client wants to omit some of the categories on one of their product archives.
eg. Product category page 1 they only want to show Tables, Chairs.
All other product categories will show the full catalogue, Tables, Chairs, Cushions, Benches, Sets, etc. etc.
I've got the conditional statement in my template so can use two separate shortcodes, but it seems that I can't just select certain categories to show in a shortcode. Is this correct?
As a test I've tried to add a filter using the hook 'woof_get_tax_query' to omit a category ID 169 by adding this to my functions.php - It has no effect.
add_filter('woof_get_tax_query', 'exclude_product_category_from_woof', 10, 1);
function exclude_product_category_from_woof($tax_query) {
foreach ($tax_query as $key => $query) {
if ($query['taxonomy'] === 'product_cat') {
$tax_query[$key]['terms'] = array_diff($query['terms'], [169]);
}
}
return $tax_query;
}
Am I in the correct ballpark? :)
Many thanks
Byron
Quote from Pablo Borysenco on May 29, 2024, 12:26Hello Byron
If you want to change the displayed products then you need to change the request in the template (not in the filtering plugin). so it depends on the template you use to display the products
to exclude categories from the filter you can use this hook - https://products-filter.com/hook/woof_sort_terms_before_out
Hello Byron
If you want to change the displayed products then you need to change the request in the template (not in the filtering plugin). so it depends on the template you use to display the products
to exclude categories from the filter you can use this hook - https://products-filter.com/hook/woof_sort_terms_before_out
Quote from Byron on May 29, 2024, 13:43Hi Pablo, thanks for the quick response :)
I'm trying to use the hook to exclude product cat ID 169 (Chairs) just to test the code but the category is still in the list. I've removed all excludes from the plugin settings. Sadly it's not working.
function my_exclude_categories($terms) {
// Define the category IDs to exclude 169 Chairs
$excluded_category_ids = array(169);// Filter the terms, removing the excluded categories
$terms = array_filter($terms, function($term) use ($excluded_category_ids) {
return !in_array($term->term_id, $excluded_category_ids);
});return $terms;
}add_filter('woof_sort_terms_before_out', 'my_exclude_categories');
Any more advice?
Thanks,
Byron
Hi Pablo, thanks for the quick response :)
I'm trying to use the hook to exclude product cat ID 169 (Chairs) just to test the code but the category is still in the list. I've removed all excludes from the plugin settings. Sadly it's not working.
function my_exclude_categories($terms) {
// Define the category IDs to exclude 169 Chairs
$excluded_category_ids = array(169);
// Filter the terms, removing the excluded categories
$terms = array_filter($terms, function($term) use ($excluded_category_ids) {
return !in_array($term->term_id, $excluded_category_ids);
});
return $terms;
}
add_filter('woof_sort_terms_before_out', 'my_exclude_categories');
Any more advice?
Thanks,
Byron
Quote from Pablo Borysenco on May 30, 2024, 10:14Hello Byron
I recommend adding a taxonomy check to only check categories
and use $term['term_id'] instead of $term->term_id
Please clear all cache and in Advanced+Options - https://share.pluginus.net/image/i20240530110317.png
Hello Byron
I recommend adding a taxonomy check to only check categories
and use $term['term_id'] instead of $term->term_id
Please clear all cache and in Advanced+Options - https://share.pluginus.net/image/i20240530110317.png