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.
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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Get Avatar Image URL WordPress
- How to Add WooCommerce Storefront Product Pagination
- How to Change Shop Page Title In WooCommerce
- How to Hide the WordPress Admin Bar Programmatically Without Plugin
- How to Remove Built with Storefront & Woocommerce Footer Link with 4 Simple Options
- How to Add Product to Cart Programmatically in WooCommerce
- How to Link to an Email Address in WordPress
- How to Redirect Return to Shop to Custom URL In WooCommerce
- How to Change WooCommerce Checkout Labels
- How to Create WooCommerce After Login Redirect to Homepage
- How to Change Post Date Programmatically In WordPress
- How to Change Out Of Stock Text in WooCommerce
- How to Set Up WooCommerce Terms and Conditions Checkout
- WooCommerce Create Custom Single Product Page
- How to Add Amazon Affiliate Products to WooCommerce
- How to Add Text Before the Price in WooCommerce » Add Text Before Price
- How to Get Current User Role in WordPress & Display Roles
Comments are closed.