Show only relevant tags on a WooCommerce product category page

Viewed 161

We've created a custom function to show all product tags on a product category page. The idea behind these tags are, that people can 'enable' and 'disable' them to show results.

The problem we're facing right now, is that all the tags of the webshop are showing, instead of the relevant options that could be enabled.

To give an example, on the category page "Baby & Peuter", you can see the tags above the product grid : https://cadeaumatch.nl/product-categorie/baby-peuter/

When you 'enable' the tag "Altijd goed" (by clicking on the plus icon), there wont be any results because this tag isn't relevant for this category page.

This is our current code to show the tags :

$cat_args     = array(
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => false
);
$product_tags = get_terms('product_tag', $cat_args);

foreach ($product_tags as $tagg => $tagval) {
    if (in_array($tagval->slug, $excludedTags)) {
        foreach ($excludedTags as $key => $excludedTag) {
            if ($tagval->slug === $excludedTag) {
            
        }
    }
} else if (in_array($tagval->slug, $includedtags)) {
    foreach ($includedtags as $key1 => $includedTag) {
        if ($tagval->slug === $includedTag) {
            
        }
    }
} else {
    echo '<span class="spanTags" id="' . $tagval->slug . '"  style="background-color: #f1f2f6; color:black;"><span  onClick="selectChangeIn(\'' . $tagval->slug . '\')"><i class="fas fa-plus" style="color:green"></i></span>  <a href="' . get_term_link($tagval, 'product_tag') . '" style="color:black">' . $tagval->name . '</a> <span  onClick="selectChange(\'' . $tagval->slug . '\')"><i class="fas fa-minus" style="color:red"></i></span></span>';
    
}
};

Is there anyway to customize this code, to only show relevant tags on the category page? So that a visitor can't see tags that won't work on that specific category.

Thanks in advance!

0 Answers
Related