How to Change Return to Shop Link in Woocommerce

After completing the WooCommerce setup one of the most common things that you will want to change or remove is the ‘Return to Shop’ link redirection.

Finding a quick solution for this problem can be difficult for WooCommerce beginners but after reading this post you will realize that this is a very simple issue that you can solve without the need of a WooCommerce developer.

For those curious users who don’t want to just copy-paste the code in the function.php but want to understand what it does, I will explain in detail.

If you are a WooCommerce WordPress developer you may also want to check out this other tutorial on redirecting WooCommerce users based on the role after registration.

Return to Shop Link

The return to shop link can be seen when the cart is empty as shown in the image above.

The return to shop link can either be a button or a link depending on the theme you are using. By default, it is just a link that redirects to the shop when clicked.

How to Change Return to Shop Link in Woocommerce

To change this ‘return to shop ‘link we need to use a filter hook that filters the content that is displayed on the page and modifies the content to display what we want visitors to see.

WordPress works with hooks that attach to different WordPress events and are fired when those events occur.

There are two types of WordPress hooks, the filters, and the action hooks. The filters are used to filter the content and modify it before it can be returned for display.

Changing the Return to Shop Link

The most common change for the ‘Return to Shop’ link is the page where it is redirected.

Luckily, WooCommerce comes with a filter that allows users to quickly change the redirected page.

The filter  woocommerce_return_to_shop_redirect  makes it easy to add a new landing page where users are redirected when they click this page.

To use the filter we need the add_filter() function with a callback function that executes the redirect.

So we need to add_filter() in the functions.php file the first parameter is the woocommerce_return_to_shop_redirect   filter and the second parameter is the callback  function as shown in the code below :

add_filter( 'woocommerce_return_to_shop_redirect', ‘custom_empty_cart_redirect_url' );

We now need to create the callback function with the URL where we will redirect the users when they click the link:

function custom_empty_cart_redirect_url(){

return 'http://yoursite.com/page-example/';

}

This callback function will redirect the link to the new page that is inside this function.

Redirect the Return to Shop Link to the Previous Page 

You can also redirect the user to the previous page by replacing the return value in the code above with $_SERVER[‘HTTP_REFERER’]

In this case, the callback function code will be as shown below :

function custom_empty_cart_redirect_url(){

return $_SERVER['HTTP_REFERER'];

}

Redirect the Return to Shop Link to the Home page

If you wish to redirect the return to shop link to the home page, you can change the function return value to home_url() and the callback function code will be as shown below :

function custom_empty_cart_redirect_url(){

return home_url();

}

Conclusion

These snippets should be copied in the theme function.php file and the complete code should have both the add_filter() and the callback function as shown below :

add_filter( 'woocommerce_return_to_shop_redirect', ‘custom_empty_cart_redirect_url' );

function custom_empty_cart_redirect_url(){

return 'http://yoursite.com/page-example/';

}

Do not forget to change the return value of the callback function to the destination you want users to be redirected to.

I hope you find this tutorial useful and you can also get in touch with me for more help on WooCommerce and WordPress development.

Similar Articles

  1. How to Get Current User Role in WordPress & Display Roles

Comments are closed.