Product Category Permalinks in WooCommerce – pfew that's a mouthful. And it sounds so boring and technical. I do occasionally get asked some questions about WooCommerce permalinks and a little understanding of how permalinks work in WooCommerce should explain why we create them that way.
It makes logical sense to sell a t-shirt and to categorize it under clothing. If each product is in one and only one category then you shouldn't have any permalink confusion. But what happens when you're selling a book like Zen and the Art of Motorcycle Maintenance? Should it go into the zen category or the motorcycle category?
Now things get a little tricky. Since this is the virtual world (unlike the library) we can put the product in both categories. With WooCommerce you can navigate to Zen and the Art of Motorcycle Maintenance book via either category since. Unfortunately search engines are a different beast…
One Permalink to Rule Them All
For search engines you want to have only one permalink. Otherwise you start running into issues with Google and duplicate content. So no matter how many categories your products are in WooCommerce only creates one permalink for the product.
If you take a look through the code you'll see we're using the standard WordPress
get_the_terms function. What does this mean? It means that you might not be able to choose the
zen category to use in your permalink structure for Zen and the Art of Motorcycle Maintenance. It's whichever result is returned by
get_the_terms. You could get
zen or you could get
motorcycles. This could be a pain for users who are trying to optimize for certain keywords.
Filters to the Rescue
WordPress being what it is already has filters for this case. You can look into the
get_the_terms filter in the
get_the_terms function (yes they share the same name) and write your own filter to remove certain options from the function. Just remove any unwanted categories and you'll be left with one category which will create the permalink for your product.
This is a little complex so if you're not a developer I recommend hiring one to help you write this filter.