How to create custom WooCommerce login redirect

Today we look into different WooCommerce redirection rules after login, logout, and registration. In addition, we investigate some options for redirection hooks after user registration. We explore custom experiences, such as redirections based on user role and the different options for WooCommerce redirects after checkout and the thank you page.

There are many key actions in WooCommerce. User registration, WooCommerce cart, order confirmation, login, and many others. Each of these actions have their own WooCommerce redirect rules. Our goal for today is to add custom rules for them and go beyond the basic usage, as we show some examples of how to fully explore the different options.

Good user experience is usually defined by the details. A good redirection page can explain what is going on, and what happens next and therefore prevents user confusion and improves conversion rates.

Although redirections are important elements, usually they can’t be defined in the admin interface, unless you are using some dedicated WooCommerce plugin. Or maybe we want custom experiences, depending on user data or WooCommerce order data, like providing custom coupon code based on order items. Therefore, our goal for today is to explore the different WooCommerce redirection hooks.

woocommerce login redirect

 

Let’s get started!

WooCommerce Redirect Users After Login or Registration

WooCommerce has many redirect actions, and among them, there are the login and registration actions. It’s incredibly easy to customize them when we use the WooCommerce redirect hooks.  These hooks allow for custom URL redirection after login, registration and many other events. Each of them have their own variables, and hook type for actions or filters.

The login redirect works as a filter. This filter passes 2 arguments, the $url and the $user.

 

 

Then we can use a function to add a custom login redirect URL. This can be used, for instance, for custom welcome pages depending on the user ID. With this code snippet you can set a special page just for your admins:

 

 

Then there’s the registration redirection. Usually, the registration works better when we use the woocommerce_registration_redirect filter:

By default, this filter has the value of either the referer (if there is any) or the “my account” page:

It’s also possible to customize it even further. You could, for example, replace the account page with a whole different URL. In it, you could have the shop and some basic user information. This allows better use of the registration, as your users will be ready to shop with no extra clicks.

 

 

WooCommerce Redirect Users After Logout

Unfortunately, WooCommerce doesn’t have a special hook for their logout action. Therefore, we need to rely on WordPress itself to do this.

Then we can use the ‘wp_logout’ hook to run our action. This hook is triggered after the user logout has been processed. In addition, we need the wp_redirect function, which is a safe method for redirecting WordPress pages. The last element is our humble exit(), to make sure no additional code is run.

 

 

It’s possible to use the referrer attribute here for additional manipulation. For instance, you could test the referrers and change the logout URL if the previous page was a WooCommerce element. But we let you play with this function now.

WooCommerce Redirect Users Based on User Role

The user role is an important element. User roles can be used for content restriction and even dynamic prices. Therefore, they are incredibly flexible.

And once again, they can improve your WooCommerce redirection rules. You can modify the previous code to add role-based redirects. A quick example would be a custom welcome page for some user roles. For instance, our wholesale customers may have a special welcome page with hidden products:

 

 

WooCommerce Redirect Users After Checkout / Payment

Although there is no filter for the WooCommerce thank you page, we can still customize it. That’s because we can apply a WooCommerce redirect for the thank you action. This action is run after the new order is complete and your customer sees the regular thank you page. But we can prevent the default page from being shown by adding our own page.

The beauty of this action is its parameter, the order id.

This means that you can use order information in your custom thank you page. You can even redirect to a custom order failure page if you want to. Another example would be a custom thank you page based on the order totals, ordered product, order item, shipping method, or the payment method used.

Furthermore, we can use the wc_get_page_id and the wc_get_page_permalink functions to generate our URLs. In the example below, we redirect users to the account page in case the order was successful (the order status is not failed).

 

 

Conclusion

Today we looked into different redirection rules and how to customize them. In addition to custom WooCommerce login redirect, we saw how to create custom registration, log out and even thank you page redirections.

Furthermore, we looked into how to use WordPress functions to get URLs and always keep them updated. Using WordPress functions to get URLs ensures that they are always valid as well.

We hope you enjoyed, and see you again next time!