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.

83 replies
  1. Daniel
    Daniel says:

    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 !

    Reply
  2. Gene
    Gene says:

    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.

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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.

      Reply
  3. Nathan
    Nathan says:

    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

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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.

      Reply
  4. Sarah
    Sarah says:

    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

    Reply
  5. Sarah
    Sarah says:

    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?

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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

      Reply
  6. Mike
    Mike says:

    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

    Reply
  7. Steve
    Steve says:

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

    Reply
  8. Adrian Stewart
    Adrian Stewart says:

    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

    Reply
  9. Dean
    Dean says:

    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”?

    Reply
  10. Martin
    Martin says:

    Hi Nicola, is it possible that this cannot be used in alt themes? I have some trouble finding the code snippet in the functions file.

    Reply
  11. Patrick
    Patrick says:

    Hi i try to empty cart from another page, not in the cart page and Without plugin!

    Do you have a sollution for this as well?

    i did try this:
    /**
    * Clear Cart for WooCommerce
    */
    add_action( ‘init’, ‘woocommerce_clear_cart_url’ );
    function woocommerce_clear_cart_url() {
    global $woocommerce;

    if ( is_page('9') && isset( $_GET['empty-cart'] ) ) {
    $woocommerce->cart->empty_cart();
    }

    }

    but it does nothing, the page where this should come is page id 9

    Thanks
    Patrick

    Reply
  12. James
    James says:

    Hi,
    I want to use the second code snippet but as you said i dont want to redirect them to the cart page if they have just emptied their cart? what additions checks do i need to add??
    thanks

    Reply
  13. Maria
    Maria says:

    Thanks!
    Changing the “Return to shop” URL works fine.
    Maybe… do you have a solution for changing the breadcrumb URL (between Home and Cart) to the new URL too?

    Reply
  14. AJ
    AJ says:

    Hi
    Thanks for covering this, been looking everywhere for this solution. I tried the second code and unfortunately I got stuck in the empty cart loop. Being a total beginner with coding could you please give us ammeters a few pointers on the additional checks?
    Thanks in advance

    Reply
  15. James Lamar
    James Lamar says:

    Hello! I’m using a plugin called Code Snippet to implement the code and it isn’t working. The “return to shop button” is still very much in the cart page. I used the first code .

    Thanks in advance.

    Reply
  16. Rizzo Jenkins
    Rizzo Jenkins says:

    Thank so much! The code worked perfectly. I was worried at first because I don’t know mcc about code and didn’t want to make a mistake. But I added the code at the end and changed to my domain…worked perfectly!!!

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Mary,
      I checked the code again and it really should work properly. I don’t know this plugin Code Snippets, so I’m not sure if it will work with it or not.

      Try to add it to functions.php and let me know, please!

      Reply
  17. Joslin
    Joslin says:

    Hi! Tried this code and got an error that I “cannot redeclare the wc_empty_cart_redirect_url function”. Is that a theme issue or did I enter something incorrectly?

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Joslin,
      Your theme or maybe another plugin already is using a function named wc_empty_cart_redirect_url. Make sure that you didn’t write it twice in functions.php, or just change the function name to something else in this snippet.

      I checked in WooCommerce and it does not use a function with that name already.

      Reply
  18. Teri
    Teri says:

    I added the snippet and got an error that said the system reverted back to the original code because the servers couldn’t be found.

    Reply
  19. Teri
    Teri says:

    Here is the message:
    Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.

    Should I just try on a different browser? I am using Chrome right now.

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi, a different browser will not help in this case because it is a server error. Most probably you added the code in the wrong place or edited it wrong.

      Are you adding it at the end of the file functions.php in your theme’s folder? If the file ends with a ?> make sure to add the code before that sign.

      Reply
  20. Attika
    Attika says:

    Hi Nicola,
    A used this solution and it worked fine. Thanks for it 🙂
    But some days ago a just updated the woocommerce (Version 3.3.5) and now the code does not work.
    Can you help me why not?
    Maybe the WC changed something important code…???
    Thanks

    Reply
  21. jodey
    jodey says:

    I cannot get this to work!!! using following code

    function wc_empty_cart_redirect_url() {
    return ‘http://yourdomain.com/your-page/’;
    }
    add_filter( ‘woocommerce_return_to_shop_redirect’, ‘wc_empty_cart_redirect_url’ );

    always redirects to

    https://www.fcmarine.co.uk/shop/

    placed in fuction.php

    want ulr: https://www.fcmarine.co.uk/fc-marine-products-page/

    so..

    function wc_empty_cart_redirect_url() {
    return ‘https://www.fcmarine.co.uk/fc-marine-products-page/’;
    }
    add_filter( ‘woocommerce_return_to_shop_redirect’, ‘wc_empty_cart_redirect_url’ );

    but still no joy

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Jodey,
      I’m not sure why it is not working. The code is correct, and it’s so simple that there’s no margin for errors.

      Please try switching to the theme TwentySeventeen and see if you can reproduce the issue. if not, the issue is with the theme you are using. In this case you would need to contact the theme author, inform them about the issue and ask them to fix it.

      I also suggest to disable all non Woo-related plugins and check the issue. If you can’t reproduce it, enable the plugins one by one and see which one is causing the issue.

      Reply
  22. David
    David says:

    Thanks for the snippet!

    Say you had multiple user roles that you wanted to take back to their appropriate dashboards, do you know how the code would look to achieve that? Thanks for your help!

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.