My WordPress site uses Woocommerce for products to sell. But when you assign multiple product categories to a product and visit the product page (with Product Permalinks
set to Shop base with category
), something unexpected is happening. The breadcrumb link to the (parent) category is always the same one and does not reflect the navigated url. (This is the same problem as described here.)
Problem in summary:
Navigate to | Breadcrumb link | Expected breadcrumb link |
---|---|---|
/shop/category-1/sample-product/ |
category-2 | category-1 |
/shop/category-2/sample-product/ |
category-2 | category-2 |
I found no real answer on any source that fixes this. (I think it should be fixed, but some might say it’s for SEO reasons, preventing duplicate site contents.) So to help anyone searching for the answer, here it is:
2
Answers
What's going (wr)on(g)?
The breadcrumbs on the product page are produced by the
woocommerce_breadcrumb()
function. Which in turn gets it throughWC_Breadcrumb->generate()
.Traversing further in the code, you end up inside the
add_crumbs_single()
function where thewoocommerce_breadcrumb_product_terms_args
filter is applied to the ordering of the product terms fetch. (Seewp_get_post_terms
andWP_Term_Query::__construct()
for more info on this.)It's clear from the code, that they prioritize the term with the highest
parent
value, in other words, the term with the latest added parent in the database.Solution 1
Since this will always be the same for this product, you might want to add a filter to your theme's
function.php
(or using a custom plugin) that will overwrite this behavior. I got mine working using this:The rest of the
add_crumbs_single()
function will take care of traversing the category's parents etc up 'till Home.Solution 2
Alternatively, you could use the
woocommerce_breadcrumb_main_term
filter to change the 'main term' used for the breadcrumb trail. The filter function receives 2 arguments: the main term (asWP_Term
) and an array ofWP_Term
s with all product categories found. And it returns one term, so you can search through the array and pick the right one you want to start the breadcrumbs with.Hope this helps anyone searching for hours and working through lines of source code..!
Extra: Fix permalinks on archive pages as well
For tackling the same problem on the product archive pages, you can hook into the
post_type_link
filter and pre-emptively replace the%product_cat%
part of the permalink with the correct slug like so:Explanation
As @Markos mentioned, we tried to figure out the most logical and intuitive way to do this.
Basically, it checks if the current product category (slug from the url) is directly associated with the displayed product. If so, use that slug for the link to the product page. (Using one of the solutions above, the breadcrumbs reflect the url path for intuitive navigation.)
If the current viewed product category is not directly associated with the product, it looks for the first matching descendant (sub) category and uses that slug for the link to the product page.
This way, the user always sees how they came on the current product page and can easily navigate back to the category.
Note: If this snippet doesn't change anything, try an earlier hook priority and/or check whether the
$post_link
variable contains the%product_cat%
template part.After a lengthy chat with Philip and troubleshooting scenarios when a shop has multiple layers of categories (thank you Philip) I found the following approach for the permalinks working for me.