Posted on

Change the “Return to shop” button URL in the cart page

When the cart is empty, it shows a button to “Return to shop”.
By default, as it says, it returns to the shop page selected in the settings. But you can change that URL to something else.

To do so, add this code to your functions.php file located in wp-content/themes/your-theme-name/:

This will redirect to the URL specified in the code.
What if you want to dynamically return to the page previously visited?

Use this code instead:

Be aware that it will return to the previous visited page. So if you were on the cart page while it was not empty, and then removed everything in it, the referrer URL will be the non-empty cart page.

You would need to add some additionals check to be sure that the referrer URL is not the cart page.

36 thoughts on “Change the “Return to shop” button URL in the cart page

  1. Hi, thanks so much for sharing this ! I’ve been looking for months to get that “previous page” link trick !! Worked perfectly, thank you thank you !

    1. Hi Daniel!
      Thanks for the feedback! I’m happy that my post helped!

  2. I can not find the file functions.php tell me where he is?

    1. It is located inside your theme folder, in wp-content/themes/your-theme-name/ where your-theme-name is the name of the active theme.

  3. It works!
    Thanks alot buddy! Really appreciate.
    🙂

  4. Copy and pasted the code snippet directly and doesn’t work. Instead, it shows the line of code on the top of my website when visited.

    1. Hi Coy,

      Instead, it shows the line of code on the top of my website when visited.

      That means that you pasted it in the wrong place. If at the end of your functions.php file you have the sign ?> make sure to paste the code before the sign.

      1. THAT helped a LOT
        thank you!

  5. Thanks for the code.
    For some reason after installing the code, when I click on the “Return To Shop” button, the cart page just refreshes and I am not carried to the URL that I placed in the code for the re-direct.
    Gene.

    1. Hi Gene,
      Did you use the first or the second snippet?

      The second will redirect to the cart again if the previous page visited is the cart page.

      For example, if you visit the cart, then delete an item in it, you get the Return button.

      In this case the previous page visited is the cart page, so it will redirect always there.

  6. Hi, what to do in case that I have Polylang plugin and need to define 2 different links? the default language work perfect, the problem with the 2nd lang.
    Thanks

    1. Hi Nathan,
      I’m not sure about Polylang, I never used it. With WPML you would check the constant ICL_LANGUAGE_CODE and based on that you would return a different URL.

      Something like:

      if ( ICL_LANGUAGE_CODE == 'it' ) {
        return "Italian URL";
      } else {
        return "English URL";
      }
      

      You can change the part with ICL_LANGUAGE_CODE with what Polylang uses to detect the current language.

  7. Hey man

    I know its an old post, but when i add your code to the function.php file i get a blank page, any ideas why?

    1. Hi Evan,
      Thanks for writing!

      It means that there’s an error somewhere. Can you check your error log an see if there’s any Fatal error entry?

  8. Hi

    I have used the first code and put in the address for our shop page, but now when I empty the basket I do not get a return to shop button….any ideas why?

    Thank you for your help

    1. Hi Sarah,
      It could be a conflict with your theme.

      Try to disable it and enable a different one and see if you can reproduce the issue.

  9. Hi again

    We have a wholesale order form on our site as well as retail shop page.

    As I stated above that the return to shop button has disappeared since putting in the code.

    As there was no button to return to shop after basket is emptied, I changed the link to our wholesale order form so that when wholesale customers are on their account page and if they click on orders and have no orders there is a button which says go shop, now this link goes to the wholesale order form , which is great, but it also goes to the wholesale order form for retail customers.

    Is there any way of telling it to go to our shop page for retail customers and not to the wholesale order form?

    1. Hi Sarah,
      I’m sorry but I’m not sure to understand how the website works. I don’t know how you create this contact form, therefore I’m not really able to help you with that 🙁

      Sorry

  10. How would this work with a multi-site install when the site name cannot be hard coded in code?

    1. Hi Paul,
      You can use the function get_blog_details() to retrieve the blog data.

  11. Doing this exactly as the article describes (copy and pasted) outputs the following warning in the href of the return to shop link:

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘wc_empty_cart_redirect_url’ not found or invalid function name in /wp-includes/plugin.php on line 235

    1. Hi Mike,
      That error means that the system can’t find the function wc_empty_cart_redirect_url. I guess it is in the wrong place or partially copied.

  12. Great bit of code – I love the internet – can usually find the answer out there somewhere!
    Thanks for the code and the easy explanation on how to use it (one day i may even learn php …. until then .. i will rely on good people like yourself!)

    1. Awesome Steve, thanks for the kind words!

  13. Thanks, great code!

    1. You’re welcome Femke!

  14. Thank you for this code, was a big help!

  15. hi, im using a divi them, im not sure where to paste the code, above you mentioned that it should go just before ?> but i dont have that at the end of my functions.php, So can i paste it anywhere? Thanks in advance

  16. Nevermind, i tried just adding it to the bottom and it worked, thanks so much 🙂

    1. Yeah if there’s no ?> just add the code at the end of the file!

      I’m happy you figured that out!

  17. Thank you for the life saver!

    Can you share with us if there is a way to rename the button? Ie instead of “Return To Shop” I would like to call it “Back To Plans Overview”?

    1. Hello Dean,
      The only way to do that is to override the template cart-empty.php from WooCommerce to your theme, and then edit the text in your theme’s template.

      You can learn how to override templates in WooCommerce here: https://docs.woocommerce.com/document/template-structure/

  18. Really great thanks! The 2nd snippet worked perfectly!

    1. You’re welcome Chris!

Leave a Reply

Your email address will not be published. Required fields are marked *