WooCommerce Redirect to Category Page

In WooCommerce when you add a product to your cart the product page refreshes with a little banner confirming you added the item to the cart.

WooCommerce Added to Cart Notice

The “added to cart” notice in WooCommerce

This is useful for big e-commerce stores like Amazon where users are likely to buy multiple items with every checkout. But this isn't useful for every retailer. Some retailers sell very different products and users rarely buy more than one at a time. For example if you sell computer desks like Rebel Desk (yes they totally use WooCommerce) it's unlikely that someone will buy more than desk. And in that case it may help your users to redirect them to another page. Maybe the shop page, maybe a specific category, or maybe directly to the checkout. With a snippet of code you can do that in WooCommerce.

Redirect to Shop Page

The easiest thing programatically is to redirect to the shop page. This might be useful if you have a bunch of categories for the user to browse. You can add the following snippet to your theme's functions.php with a code editor.


<?php
// credit: WooThemes https://gist.github.com/woogist/6fd2d358bb2e51a25ac5
add_filter( 'woocommerce_add_to_cart_redirect', 'patricks_custom_cart_redirect' );
function patricks_custom_cart_redirect() {
return get_permalink( wc_get_page_id( 'shop' ) );
}

view raw

functions.php

hosted with ❤ by GitHub

Redirect to Category Page

What's usually a better option is to direct the user to a more applicable category rather than throwing them back to the shop page. If you have accessories for your product and they're all lumped into an accessories category that's an excellent place to redirect the user. It's helpful for them and you usually have higher margins on those items.

Get the Product Category ID

The first thing is you have to get the ID for the product category.

Within your site go the backend and go to Products -> Categories. Then click on the name of the category that you want to use in the redirect.

WooCommerce Admin Categories Screen

Click on the name of the category you wish to use in the redirect

Copy the ID of the category which is in the URL of the edit category page.

WooCommerce Admin Category Edit Screen

Grab the ID from the URL. It immediately follows “tag_ID”.

Code

Now add the following snippet to your theme's functions.php file. With all of these snippets you can omit the opening <?php tag. That's there so the code highlighting works and it looks pretty in the browser. 🙂

Don't forget to replace the product category ID with the number we got above.


<?php
add_filter( 'woocommerce_add_to_cart_redirect', 'patricks_custom_cart_redirect' );
function patricks_custom_cart_redirect() {
// our prodcut category ID
$product_cat_id = 10; // replace this number with your category ID
// redirect to the page
return get_term_link( $product_cat_id, 'product_cat' );
}

view raw

functions.php

hosted with ❤ by GitHub

The Result

WooCommerce Redirect Product Category

WooCommerce redirecting to my Accessories category.

Now no matter what product someone adds to the cart we'll be redirected to the accessories category page. That's much more useful to your users than reloading the product page.

Happy redirecting!

2 thoughts on “WooCommerce Redirect to Category Page

  1. Could you make the code generic to redirect to the category of the product just added? I realise that the code would have to decide which category when a product is in multiple categories.

    • Hey Damien,

      You certainly could do that. I would look at the get_categories method for the WC_Product class.

      If you’re a developer you can likely do this yourself. If not I would reach out to someone like Codeable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.