WooCommerce: “Continue Shopping” Button @ Single Product Page

A freelance client hired me a while ago to display a “Continue Shopping” button on the Single Product Page, next to the Add to Cart. A simple way to send their users back to where they were coming from without clicking the “previous” button on the browser πŸ™‚

Here’s how it’s done, featuring an awesome WordPress function called “wp_get_referer()”!

Continue Shopping button @ WooCommerce Single Product Page

PHP Snippet: “Continue Shopping” Button @ Single Product Page – WooCommerce

 

/**
 * @snippet       Continue Shopping button @ Single Product Page
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=72772
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.1.1
 */

add_action( 'woocommerce_single_product_summary', 'bbloomer_continue_shopping_button', 31 );

function bbloomer_continue_shopping_button() {
  if ( wp_get_referer() ) echo '<a class="button continue" href="' . wp_get_referer() . '">Continue Shopping</a>';
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

67 thoughts on “WooCommerce: “Continue Shopping” Button @ Single Product Page

  1. This sort of works πŸ™‚ It places an appropriate button on the product page ok, but it disappears if you press “Add to Cart”. Ideally for me that should be the other way around so that the button appears after pressing add to cart.. Even better if that can be placed in the success message.

    1. I see, I guess I use it in combination with “Redirect to cart upon add to cart” so the problem is not happening in such case

      1. This is not working now – is there an update to this snippet that will work with the latest version of WooCommerce? Thanks!

        1. Hello Anji, was it working before for you?

  2. Hi, it was working fine until I added a side bar and the “continue shopping” button no longer appears. can you help!?
    I am using the free Astra theme.

    1. Hi Sabrina, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. the same problem, β€œcontinue shopping” button no longer appears.

        1. Same theme? Have you tried with a different one already?

  3. Hi Rodolfo,
    I have your snippet working.
    I would like to display that button just at the right of “Add to cart”, instead of below it.

    Thanks.

    1. Just need some custom CSS there

  4. If you are looking for a cart button redirecting to the last product included in the cart, check out this:

    add_action( 'woocommerce_proceed_to_checkout', 'bbloomer_continue_shopping_button', 31 );
    function bbloomer_continue_shopping_button() {
      $items = WC()->cart->get_cart(); 
      $product_id = end($items)['data']->get_id();
      echo '<a class="button continue" href="'.get_permalink($product_id).'">Continue Shopping</a>';
    }
    

    You can thank me later πŸ™‚

    1. Thank you Chris!

      1. Hey Chris,

        I’m loving this code, but would you know how to get it to go back to the category page for the last product added, instead of back to the product itself?

        I can’t work it out – it would be super amazing if you could!

        Thanks πŸ™‚

  5. Hi Rodolfo, thanks for your helpful advice.

    Do you think it is possible to have two separate buttons that do:
    1) add to the cart without redirecting to the cashier (they continue in the catalog)
    2) add to cart with automatic redirection to the cashier

    Thanks for the help you can give me
    Gabriele

    1. Hey Gabriele, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Made some corrections πŸ™‚
    Now it goes back to previous page or after adding to cart to a shop page

    add_action( 'woocommerce_single_product_summary', 'bbloomer_continue_shopping_button', 31 );
    function bbloomer_continue_shopping_button() {
      if ( wp_get_referer() ) echo '<a href="' . wp_get_referer() . '" rel="nofollow">Continue Shopping</a>';
    	  else echo '<a href="./shop" rel="nofollow">Continue Shopping</a>';
    	
    }
    
    1. Thanks!

  7. Hi Rodolfo,

    Thank you for this post. However, the code snippet dsplays only the text link. Is it possible to make it a css styled button ?

    Cheers

    1. Of course – just add a class to its HTML

  8. Hi, I’m using Flatsome and Flatsome child-theme. All my products are with several variants. On my site this snippet won’t work, unfortunately.
    Thanks for all the work you put into all the snippets. Returning to your site very often to make sure I didn’t miss anything fun.

    Best regards
    Jan-Ove Sweden

    1. Hi Jan, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

  9. Hello! I want to use this code but with a different hook? I want the button to appear only after something has been added to the cart? Also, through messing around it seems if the user goes to cart and then uses the product link to go back to view product when you click the “continue shopping” button it will direct you back to the cart instead of the shop page!

    1. Hello Jennifer, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  10. Hi Rodolfo
    I have placed your code on my code snippet plugin, like this:

     add_action( 'woocommerce_after_cart_table', 'bbloomer_continue_shopping_button', 31 );
     
    function aby_continue_shopping_button() {
      if ( wp_get_referer() ) echo '<a href="' . wp_get_referer() . '" rel="nofollow">Continue Shopping</a>';
    }
    

    but returns the following error:
    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘bbloomer_continue_shopping_button’ not found or invalid function name in… /public_html/wp-includes/class-wp-hook.php on line 286

    1. Function name is wrong πŸ™‚

  11. Yeah, thank you, thank you!!!
    I modified it removing the get_referrer part because I wanted it to show up even after the “View Cart” button appears.

    1. Excellent!

    2. This is exactly what I’m trying to do but am a complete amateur when it comes to hooks. I’ve been struggling trying to figure out how to accomplish having the button display even after an item has been added to the cart… would you be willing to share with me how you modified the code? Thank you!

  12. I can see the Continue Shopping button fine when hitting a single product page from the store front but if I just change the product name within the browser URL then it vanishes! Any ideas? The code is added to the child theme functions.php file.

    1. Hey Wayne, thanks for your comment! If you type in the URL, that URL will be counted as first visit to the website, so there is no “previous” page. Hope this helps πŸ™‚

  13. If you want to add continue buttons both on single porduct page and cart how do I do that?
    It does not work to add these code in functions.php

    add_action( 'woocommerce_single_product_summary', 'bbloomer_continue_shopping_button', 31 );
    function bbloomer_continue_shopping_button() {
      if ( wp_get_referer() ) echo '<a class="button continue" href="./shop">Continue Shopping</a>';
    }
    add_action( 'woocommerce_after_cart_table', 'bbloomer_continue_shopping_button', 31 );
    function bbloomer_continue_shopping_button() {
      if ( wp_get_referer() ) echo '<a class="button continue" href="./shop">Continue Shopping</a>';
    }
    
    1. Hey Pelle, thanks for your comment! you can’t re-declare a function with the same name, try changing that πŸ™‚

    2. How do you mean with change name on function, which parameter must I change?

      1. You can’t use “bbloomer_continue_shopping_button” twice, rename the second one to something else πŸ™‚

    3. Thanks Rodolfo! it works fine!

  14. Hi Rodolfo!

    Nice snippet! I just saw a friend’s website that had that option and remembered that I always thought it was a good idea! A little Google search and it brings me right back to your site. The best place for snippets!

    Just wondering though (I’m not a developer)… I see bbloomer in the code. Can this be changed to anything we want? Can it be removed completely? My curiosity is wondering why it’s there πŸ˜€

    1. Hey Paul, thanks πŸ™‚ Yes, that’s just the name I gave to the custom function, you can change it to anything you like.

  15. hey Rodolfo thanks for the code. have been using a modified version to add two buttons once the product is added to the cart BUT the person im helping needs the buttons removed once you navigate away from the page of the product you just added to the cart; i.e. on any other page. Or maybe the correct term is on any single product page where that product does not appear in the cart? pleeeeease help asap- have spent hours and hours and i just dont know enough. THANK YOU!!!

    1. Shane, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

    2. Cant you please point me in the right direction? What woocmmerce tag / function to use? Im not asking you to write it but maybe if I actually new what to search for I could make it work…?

      1. I can’t, sorry πŸ™‚

  16. Hi there,

    I’m a PHP newbie. Is there a way I can add this piece of code just under the “this product has been added to your cart” success message?

    Thanks very much in advance!

  17. Hi,

    Thank you for this post, it’s a great one for me. But I have a little problem. The Continue button is above Add button, can you help me to move it a little bit down ?

    1. Eusebiu, thanks for your comment! Yes you can achieve that via CSS. Unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  18. How would I add the “continue shopping” button to the shopping cart page /shopping_cart/ ?

    1. Hey Ilana, thanks for your comment! You will need to change “hook”. Instead of “woocommerce_single_product_summary” you should use one of the Cart hooks

  19. Hello,

    Thank you for sharing this. I was wondering if you could please share a snippet for adding this button to the thank you page?

    I’m currently running a focused checkout to minimize distraction but unfortunately it doesn’t give the customer an option to keep shopping.

    Thank you in advance

    1. Hey Jonee, thanks for your comment! This tutorial will show you how to add content to the thank you page: https://businessbloomer.com/customize-woocommerce-thankyou-page-tutorial/. Hope this helps

  20. hello
    The ‘continue shopping’ button disappears when the view cart message is displayed.
    How can we keep the button in place when view cart message is displayed?
    thanks.

    1. Phil, thanks for your comment! Not sure, can you provide a screenshot?

    2. This happens to me as well. Once you click add to cart, the continue shopping button disappears. Any suggestions?

    3. When you simply display single product page, button appears as it should. After adding product to basket, button is no longer added.

    4. I fixed this by removing

      if ( wp_get_referer() )

      and just going with

      echo '<br><a class="button continue" href="./shop">Continue Shopping</a>';

      in the function.

      The reason why it was disappearing is because clicking on the add to cart button refreshes the page, and if the previous page was the same page you’re on,

      wp_get_referer() 

      will return false.

  21. Thank you so much! This works perfectly – LOVE it!

    1. Thank you Lechelle πŸ™‚

  22. This is great and exactly what I was looking for, however,

    Button with continue shopping appears on single product screen but when the item gets added to the cart the page refreshes with the added to cart message and then continue shopping button disappears.

    any ideas, anyone else has that issue.

    1. Mark, thanks for your comment! Maybe in that case you should add a condition that if “wp_get_referer();” is not available it should redirect to the default Shop page?

  23. Great this is really help full

  24. Hi Rodolfo,
    Really it’s very nice way to CONTINUE SHIPPING option. But it’s redirect to previous click page only.

    β€œContinue Shopping” button on the Single Product Page, In the previous code if you click Add to cart button after then click to continue shopping button it’s not working properly.

    I had modified herf line.

    add_action( 'woocommerce_single_product_summary', 'bbloomer_continue_shopping_button', 31 );
    function bbloomer_continue_shopping_button() {
      if ( wp_get_referer() ) echo '<a class="button continue" href="./shop">Continue Shopping</a>';
    }
    

    Continue Shopping button working good.

    Thanks
    Murthi

    1. Thank you Murthi πŸ™‚

      1. Hi Murthi,

        May I suggest to use
        get_permalink( woocommerce_get_page_id( ‘shop’ )
        instead a fixed permalink as ./shop

        Greetings.

        1. Cool!

  25. Works like magic! Thanks.

    1. Thank you so much Emmanuel πŸ™‚

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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