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.

apply_filters( 'woocommerce_login_redirect',  (string) $redirect,  (object) $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:

add_filter('woocommerce_login_redirect', 'ui_wc_login_redirect', 99, 2);

function ui_wc_login_redirect($url, $user) {
  if ($user->ID == 1) {
    $url = add_query_arg('user', 'admin', $url);
  } else {
    $url = add_query_arg('user', 'notadmin', $url);
  }
  return $url;
}

 

 

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

apply_filters( 'woocommerce_registration_redirect', $url );

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

// by default
if ( wp_get_referer() ) {
  $url = wp_get_referer()
} else {
  $url = wc_get_page_permalink( 'myaccount' );
}

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.

add_filter( 'woocommerce_registration_redirect', 'ui_custom_redirection_after_registration', 10, 1 );
function ui_custom_redirection_after_registration( $url ){
  //let's keep the url as it is if there is a referer
    if ( ! wp_get_referer() ) {
    //custom account page ID
    $url = get_permalink( 155 );
  }

    return $url; // Always return something
}

 

Unlock the Full Potential of Your WooCommerce Customer Data

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.

add_action('wp_logout','ui_auto_redirect_after_logout');
function ui_auto_redirect_after_logout(){
  wp_redirect( home_url() );
  exit();
}

 

 

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:

add_filter( 'woocommerce_login_redirect', 'ui_wc_login_redirect', 99, 2 );

function ui_wc_login_redirect( $url, $user ) {
  if ( isset( $current_user->roles[0] ) && '' != $current_user->roles[0] && in_array( 'wholesale',  $current_user->roles ) ) {
    //custom wholesale welcome page ID
    $url = get_permalink( 189 );
  }
  
  return $url;
}

 

 

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).

add_action( 'woocommerce_thankyou', 'ui_custom_thankyou');
 
function ui_custom_thankyou( $order_id ){
    $order = new WC_Order( $order_id );
 
    $url = wc_get_page_permalink( 'myaccount' );
 
    if (  $order->get_status() != 'failed' ) {
        wp_redirect($url);
        exit;
    }
}

 

 

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!