We have a WooCommerce clothing store and want to create coupons that have certain categories excluded. Our category structure is as follows (simplified):
– Men
– – Shirts
– – Jeans
– – Sale
– Women
– – Shirts
– – Jeans
– – Sale
However when we want to select [Men » Sale] from the Exclude categories dropdown picker in [WooCommerce » Marketing » Coupons » Usage Restriction] the list is flattened:
– Jeans
– Men
– Sale
– Sale
– Shirts
– Shirts
– Women
Now it is not clear which categories are actually the one we want to exclude. Have in min, this is a very simplified structure, in the live environment this is even worse.
I hoped for an apply_filter
but the get_terms()
apparently does have none in code, see source code on github here » (line #233)
$categories = get_terms('product_cat', 'orderby=name&hide_empty=0');
Since this is not filtered, I don’t see a way to add a parent category to have this as the desired outcome:
- Men
- Men » Jeans
- Men » Shirts
- Men » Sale
- Women
- Women » Jeans
- Women » Shirts
- Women » Sale
Any idea how to get that list now that there is no filter in the code?
For example, this is what we use for a third party plugin that also uses the WooCommerce category terms, but that actually does have a filter on the get_terms and the native WooCommerce code on coupons does not…
Example:
if (!function_exists('yith_wcbep_add_parent_category_name')) {
function yith_wcbep_add_parent_category_name($terms, $args) {
$term_name = '';
$taxonomy = isset($args['taxonomy']) ? $args['taxonomy'] : '';
foreach ($terms as $term_id => $term_name) {
$term = get_term_by('id', $term_id, $taxonomy);
$term_name = isset($term->name) ? $term->name : '';
if (isset($term->parent) && $term->parent > 0) {
$parent = get_term_by('id', $term->parent, $term->taxonomy);
$term_name = $parent->name . ' > ' . $term_name;
$terms[$term_id] = $term_name;
}
}
return $terms;
}
add_filter('yith_plugin_fw_json_search_found_terms', 'yith_wcbep_add_parent_category_name', 10, 2);
}
2
Answers
This was an interesting feature to solve, seems kind of strange Woocommerce doesn’t have hierarchy names in the coupons categories.
So I did some digging and found that the only appropriate filter seems to be
get_terms
, after some trial and error I came up with this.Here is the before.
And here is the after.
The general idea of this filter is to specifically work only when creating or editing a coupon on the dashboard.
Then it updates the category name to contain the whole hierarchy of the categories names.
This might not be perfect but it’s a starting point
To solve this issue, what you can do is to clone WooCommerce coupon product categories restrictions section, customizing the categories output as you like *(here I add the parent term name to the category name), removing with JavaScript the related HTML output from the default WooCommerce ones.
So you will get the following:
Here is the hooked function code:
Code goes in functions.php file of your active child theme (or active theme). Tested and works.