The Blog

How to Create WooCommerce Custom Redirects

WooCommerce is awesome for many reasons, but today I want to highlight it’s amazing ability to be easily extended and customized to suit every situation.

Specifically in regards to redirecting.

There are a number of handy filters at your disposal for the times when you wake up in the morning, the sun’s out, a coffee’s in hand and you feel like hooking into WooCommerce core.

I’ll take you through a few of them but leave some hidden, as I’m too lazy to find every single one. But needless to say, if you’re looking for one, it probably exists. If it doesn’t, great! Go and open an issue, followed by a nice PR (and check out Contributing on GitHub for Dummies).

You’ll probably want to change the $redirect variable in all of the following snippets to be the full URL of where you’d like the redirect to redirect to.

You can also be thrifty and use some PHP / WordPress core functions, like admin_url(); to take them to the admin or get_permalink( 25 ); to take users to the page/post/etc. with an ID of 25 or whatever.

After logging in

After logging out

Firstly, you’ll need to make sure that you are overriding the default WooCommerce template files in your theme. If you’ve already got a woocommerce folder in your theme’s directory, you’ll be okay to continue onwards. If you don’t see one, please first take a look at the Template Structure Documentation and ensure that the correct template files are copied to your theme.

Now let’s open up the file found in /wp-content/YOUR_THEME/woocommerce/myaccount/my-account.php.

Approximately around line 21, you will find the following line:

wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) )

We’re going to want to change this line to set a custom URL to redirect to after logging out. If, for example, you wanted to send customers to Google after logging out, you could change it to this:

wp_logout_url( 'http://google.com/' )

After registering

After adding an item to the cart

After clicking 'return to shop' on the cart page

After clicking ‘continue shopping’ after adding an item to the cart

All the above code snippets need to be added to either your theme’s functions.php file or through a custom PHP plugin like Functionality. Make sure to remove the opening <?php tag if need be.

Introducing Metorik

For

A single dashboard unifying your store's orders, customers, and products, helping you understand your data and make more informed decisions.