Patrick's Programming Blog

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.

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.

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.

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.

The Result

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!

Exit mobile version