Maybe because you have only one product category and therefore search engines would find duplicate content (Shop page = Category page) and penalize your website.
Or maybe because you use advanced product filters and you prefer customers to see the filtered view “by category” (e.g. “example.com/shop/?_product_category=tables“) as opposed to the default category pages ( “example.com/product_category/tables” ).
Either way, it is possible to programmatically redirect all product category pages to a given page or to a relevant URL with parameters – and here’s the fix. Enjoy!
PHP Snippet 1: Redirect All Product Category Pages to Shop Page
/**
* @snippet Redirect To Shop @ WooCommerce Category Pages
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_action( 'wp', 'bbloomer_redirect_cats_to_shop' );
function bbloomer_redirect_cats_to_shop() {
if ( is_product_category() ) {
wp_safe_redirect( wc_get_page_permalink( 'shop' ) );
exit;
}
}
PHP Snippet 2: Redirect Each Product Category Page to A Relevant URL With Parameters
/**
* @snippet Redirect To URL + Parameters @ WooCommerce Category Pages
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_action( 'wp', 'bbloomer_redirect_cat_to_relevant_filter' );
function bbloomer_redirect_cat_to_relevant_filter() {
if ( is_product_category() ) {
$term = get_queried_object();
$url = $term && $term->slug ? '/shop/?_product_category=' . $term->slug : wc_get_page_permalink( 'shop' );
wp_safe_redirect( $url );
exit;
}
}
On my Woocommerce store, when I click on a particular product category [Medical Consumables), it redirects to a blog category [Opinion].
This particular action only applies to this category. How can I fix this please?
Thank you.
Do they have the same slug?
Hi Rodolfo and guests.
I just added one of the scripts above to my new website. I made a slight adjustment and it now works for every category as long as you create a new page with the same name as the category. Can anyone see any issues with this and does a redirect affect SEO? Ta.
Hi there, if it works for your users, it also works for Google!
Hello there! Thanks for this!!
It happens I need to do the samething for a custom taxonomy I created for products as well.
I was able to get it working for product categories even with custom slugs, applying the following modifications:
But couldn’t do the same for a custom taxonomy (“ambient” for example).
Would you happen to know how could I do so, what paremeters I nedd to change or what’s the structure logic for custom taxonomies?
Thanks in advance!
Thank you! I believe https://developer.wordpress.org/reference/functions/is_tax/ should help
Thank you! Works great!
How do i redirect it to the category on the shop page. now it only redircts to the shop page.
Not sure I follow
Great snippet.
I would suggest using
in the conditional check. Otherwise when using a category filter on the back-end for products, it will redirect you to the front-end shop page.
E.g.
Fantastic, thank you