Alchem Alchem - 1 year ago 111
PHP Question

woocommerce product with multiple categories, active category in sidebar

I need some food for thought on this because I have had trouble finding a decent solution here.

There is a woocommerce shop, categories on the left side (sidebar) and on the right side products. A product can have multiple categories; for example, the product "Burger" is in the categories "Food" and "Fastfood".

Now on the left side we have the categories "Food" and "Fastfood" with both "Burger" in them.
I go to category "Food". On the left side in the sidebar I highlight the category food with:

(start loop going through categories)

if(get_query_var('product_cat') == $cat->slug) - echo <li class="active">
else - echo <li class="active">

(end loop)

so the category "Food" is highlighted. Then I select the product "Burger".
Now, because the product "Burger" is also in the category "Fastfood", the category "Fastfood" is displayed as active.

The active category should be based on the category that the product was selected from originally ("Food"). If you go to the category "Fastfood" and select "Burger", then "Fastfood" should be active. Open "Burger" from "Food", "Food" should be the active category.

My attempt is to change the category in the permalink
(<?php the_permalink(); ?>)
to the current category when displaying all the products, but it just doesn't feel like the right way.

Maybe someone has a better solution.

Wish you a good day!


I've managed to create the permalink with the right category:

$custom_permalink = get_settings('siteurl')."/".get_query_var('product_cat')."/".basename(get_permalink());

Sadly this does not work.. (redirects me to the "wrong" category) but I don't like this hack anyways :)..

Answer Source

I got it working. This is what I did:

In your custom template file /yourtheme/woocommerce/content-product.php you will change the a href.

Code that generated the new permalink (using the current selected category):

    if(get_query_var('product_cat') == ""){
        $product_categries = get_the_terms( $post->ID, 'product_cat' );
        foreach ($product_categries as $category) {
            $cur_cat = $category->slug;

        $custom_cat = $cur_cat;
        $custom_cat = get_query_var('product_cat');

    $custom_permalink = get_permalink(5).$custom_cat."/".basename(get_permalink());

I also check if a category is set (because there is none if you are viewening "All Products", in this case I loop through the Products and get their category to use it in the permalink).

The get_permalink with the id 5 is basically my shop-page, so it stays dynamic when you change the permalink from your shop. Don't like hardcoded stuff here because it's already an ugly hack.

Place this code before the a href that wraps your Product, in my case this was on line 42 (Woocommerce V2.0.10). Then change this a href from

<a href="<?php the_permalink(); ?>">


<a href="<?php echo $custom_permalink; ?>">

I hope this will help you when you face the same issue!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download