WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart

This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology.

Here’s how we show a simple message on the WooCommerce Cart page. Enjoy!

WooCommerce: get free shipping if you add "x" money
WooCommerce: get free shipping if you add “x” money

PHP Snippet: Show Remaining Amount to Reach Free Shipping Threshold @ WooCommerce Cart

/**
 * @snippet       $$$ remaining to Free Shipping @ WooCommerce Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );
 
function bbloomer_free_shipping_cart_notice() {
 
   $min_amount = 50; //change this to your free shipping threshold
  
   $current = WC()->cart->subtotal;
 
   if ( $current < $min_amount ) {
      $added_text = 'Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
      wc_print_notice( $notice, 'notice' );
   }
 
}

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: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]
  • WooCommerce: Display Total Discount / Savings @ Cart & Checkout
    If you love Ecommerce as much as I do, and are passionate about Sales Conversion Rate and reducing Shopping Cart Abandonment, today’s snippet will come in handy. Besides, this is officially the first guest blog on Business Bloomer (have ideas? Send me your proposal here)… so let me officially introduce you to today’s author: Jamie […]

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

227 thoughts on “WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart

  1. Can we show this as a progress bar ? Any Idea on this ?

  2. Hi Rodolfo, hope you’re doing awesome. Thanks for sharing this. I used this snippet together with this detect country snippet https://www.businessbloomer.com/woocommerce-detecting-current-user-country-geolocation/ to add a message to the YITH Add To Cart Popup plugin. 🙂 Works like a charm (Woo 7.7.2).

  3. Hey, thank you so much for the snippet! How can I change this to normal text instead of a wc cart notice? Because I want to put it as text, the notice makes it a little bit problematic for me.

    1. Maybe with:

      echo $notice;
  4. Hello!
    thanks for your great snippet, but is there a way to display the message only for specific country, because i don’t have free shipping for every country. thanks!

    1. Hello Achmet, 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!

  5. The math is not showing correctly for me. The total value when added up is $49.87 but should be $99.

    1. Taxes maybe? Or does that total include shipping? Let me know

  6. Hi,
    nice work, it works fine.
    Is it possible to add the notice before the header cart button too?
    greetings
    MP

    1. Hello MP, 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!

  7. Hi!
    Your code is very good. Thanks.
    But i have a problem with wpml. I did the translation of that text ( ‘Add ‘ . wc_price( $min_amount – $current ) . ‘ worth of goods to your order to get free shipping!’), but each time, when the SUM is changing, the wpml see this like a new text.
    Other words, WMPL sees this like on sentence.
    Example:
    Add €69,02 worth of goods to your order to get free shipping!
    Add €39,02 worth of goods to your order to get free shipping!
    This is two different strings for wpml.

    Meybe this is a simple solution? WPML suggested to read this https://wordpress.stackexchange.com/questions/131248/what-if-i-have-to-pass-in-a-variable/214604#214604. I tied something, but no results.

    add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );
      
    function bbloomer_free_shipping_cart_notice() {
      
       $min_amount = 150; //change this to your free shipping threshold
       
       $current = WC()->cart->subtotal;
      
       if ( $current < $min_amount ) {
          $added_text = __( 'Add ' . wc_price( $min_amount - $current ) . ' worth of goods to your order to get free shipping!');
          $return_to = wc_get_page_permalink( 'shop' );
          $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
          wc_print_notice( $notice, 'notice' );
       }
      
    }
    
    1. Hey Mihhail 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!

  8. It works like a charm, however i wanted to add it on the sidebar mini cart of hello elementor theme.
    so i added another add_action like this ->
    add_action( ‘woocommerce_before_mini_cart_contents’, ‘bbloomer_free_shipping_cart_notice’);

    and it works(https://prnt.sc/1rkp9e8), but whenever i add this line, my cart will no longer update thought ajax and it stays at this state-> (https://prnt.sc/1rkpbh1)

    i need to refresh the page to update

    and when i remove the above line it works again like normal

    any idea?

    1. Hi Fotis, 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!

  9. Hello, thank you for this! It works great, both on desktop and mobile, but it’s not really optimized for mobile (at least on my site). It would be great if the text and button could be in different columns stacked on top of each other. When I test it on desktop and resize the window it’s fine and stacked, but on actual mobile, not.

    Any thoughts on how to troubleshoot this?

    Thank you!

    1. Hello Dave, I take you need a bit of CSS there

  10. Hi, it’s great code but i’ve got a problem. I want that free shipping notification appear on my order page of woocommerce. In what part of the code i can change ?

    Thanks

    1. Hi JM, 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!

  11. This is a great little feature!

    A problem however is that it only works when free shipping is applied to the pre-discount subtotal.

    It would be great to see an option for a post-discount subtotal. Cheers!

    1. Hi there, 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. Hi there,

        Thanks for the code snippet.
        n my case it works fine first but when for example I refresh the cart (not the page) for some reason it will swap the two lines befor and after the X amount.
        So to make it easy to understand: First i see ” Get free shipping if you order X amount more”
        Then i encrease one items quantity in the cart then refresh it, it will appear like: more X amount Get free shipping if you order! It swaps the whole thing up.

        Any suggestions what to correct in the snippet to display the text correctly after the cart refresh.

        Thanks in advance for your answer.
        Csongor

        1. Hi Csongor, try switching theme and disable all plugins but Woo and test again

  12. Hi! Thanks for yout post! 🙂 Do you know how we can customize the woocommerce cart notices style like the only…for free shiping or Click to add a coupon… The background, text etc?

    Thanks again!!

    1. Hi Lisa, 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!

  13. Rodolfo, I discover your code and as always you are the best 🙂

    This is just a thank you message, you helped me a few times and you don’t know it so : Thank you 🙂

    Kevin

    1. Cheers to that

  14. LOVE you work Rodolfo!
    Can this snipppet be added via the Code Snippets plugin?
    (Just want to be sure so I don’t the the white screen of death lol).
    Thanks!

    1. Yes of course. Code Snippet now does PHP validation so it will give you a notification and won’t break your site

  15. This is what I’ve been looking for! Nice work!
    Is it possible to display it in the cart dropdown when you hover over the shopping basket icon? I want it to be display above the products that you have in the cart?

    1. Hi Philip, 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!

  16. Hi, is there any way this code can be edited for free delivery depending on the amount of products in the cart, rather than the amount?

    Thanks

    1. Hi Kelly, 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!

  17. Hello,

    thank you, this code helped me a lot. Is it possible to call this function and display it inside popup? For example if I use boxzilla could i use some kind of shortcode to display it inside it’s popup window?

    1. Hi Matija, 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!

  18. Hi Rodolfo! I wanted to share an idea with you.. it would be great if you could combine this snippet with the one you posted here: https://businessbloomer.com/woocommerce-hide-shipping-options-free-shipping-available/

    So that the message “You only need $$ to get to free shipping” only appears where Free Shipping IS NOT available (free shipping may be available even if you don’t reach the minimum price, for example via subscription or coupon).

    1. Hello Maria, 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!

  19. Thanks for this code!

    I have one question… I have two product categories on my site :

    1. Click & Collect
    2. Standard products

    Is it possible to exclude products from the Click & Collect category, and not show the message if that’s all that is in the cart?

    Thanks!

    1. Hi Daniel, 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!

  20. Thank you so much for this ! It worked perfectly.

    I also added :

       if ( $current &gt;= $min_amount ) {
             $added_text = 'Congratulations - Your shipping is now on us and absolutely free :)';
             $return_to = wc_get_page_permalink( 'shop' );
             $notice = sprintf( '<a href="%s" rel="nofollow ugc">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
             wc_print_notice( $notice, 'notice' );
          }
    

    As a bit of a thank you to the customer 🙂

    1. Nice!

  21. How can I remove the “Continue shopping” button?

    1. Use this:

      $notice = sprintf( '%s', $added_text );
  22. Seems as of a couple updates ago its now also calculating in the tax amount as the free shipping calculation. Can you see if this is happening on your side too?

    Thanks!

    1. Hi there, if that’s the case you should play with “WC()->cart->subtotal;” and change it to something else – let me know

  23. Hi and thanks for the code which seems to age very well.

    I’m trying to use it with wpml and two different $min_amount, one for local destinations and the second for Europeans ones.

    I’m no coder, more like a copy’n’past guy and I tried this, with no luck

    By the way, I don’t care the virtual products stuff but, without it, nothing seems to work.

    Any help will be appreciated. thanks in advance,
    Frankie

    1. Hi Frankie, 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!

  24. Is there a way to make it work when I have FREE Shipping IF you have reached 30 $ in the cart OR you put the second item in the cart?

    1. Hi Nikola, 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!

  25. This code was great with base currency but did not work with other currencies. The number value did not change only the currency symbol. So basically the spend amount was totally inaccurate when currency changed from base currency. Using Currency Switcher for WooCommerce Pro

    1. Thank you Rachel. Yes, this only works for default WooCommerce – otherwise adapting it to third party plugins is 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!

  26. How can I show it based on the value entered in the admin panel, not the minimum value?

    Shipping Zone 1 = GERMANY – MİN 25
    Shipping Zone 2 = TURKEY – MİN 50
    Shipping Zone 3 = ENGLAND – MİN 30

    When Germany is selected 25, England is selected 30.

    1. Hi Gurkan, 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!

  27. Hi,

    I have added the code to the php file but i can’t find any css to add on this page, could you post it?

    1. Hey Howard, thanks for your comment – there is no CSS to go with this PHP snippet, so you’re good to go 🙂

  28. Is there a way to turn this off if a shipping class is present in cart?

    1. Hey Connor, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know 🙂

  29. Hello!

    Is it possible to display this message on the product page and replace the message when the item was added to the cart : “The product was succesfully added to your cart ” ?

    If yes could you provide me little instructions. Or if it is more difficult please e-mail me the price for it to complete the task.

    Thanks!

    1. Hello Ats, 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. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  30. Hi Rodolfo

    Is there a way to display a Message once the minimum amount has been reached?
    “Congratulations! Your order qualifies for Free Shipping”

    Thanks for your help.

    1. Hello Miguel, 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. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  31. Hello Rodolfo, great snippet. I have a problem, I set free shipping at 29 euros and everything works fine. But I also have product kits that would require free shipping even under 29 euros. I have assigned to these kits a shipping class and I use a plug-in that assigns free shipping. The problem is that your snippet is based on the amount of the cart and continues to say that you miss a few euros to reach free shipping. Is it possible to make the snippet work also based on a shipping class regardless of the amount shown on the cart?

    1. Marco, 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. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  32. Thank you for your great snippet. I am new to this and love your references.
    Everything is working for me except that it only seems to take in count the first minimum Order amount of the first Shipping Zone (25.-).

    Shipping Zone 1 = CHF 25.-
    Shipping Zone 2 = CHF 29.-
    Shipping Zone 3 = CHF 30.-

    What am I doing wrong? Is somebody else having this problem?

    1. Hey Samuel, thanks for your comment! This snippet gets the cheapest free shipping threshold:

      $min_amount = min($min_amounts);
      

      …so the coding would need to be customized in case you wanted to display a conditional message based on the current shipping zone. Hope this helps!

  33. this snippet in the last woo version include ?

    1. Yes, works on 3.4.2 🙂

  34. Hi Rodolfo,

    Thank you a lot for sharing this. The version 2.6+ had 2 bugs that are fixed here:

    
    function bbloomer_free_shipping_cart_notice_zones_static() {
     
    // Define min_amount
     
    $min_amount = 50;
     
    // Get Cart Subtotal inc. Tax excl. Shipping
     
    $current = WC()-&gt;cart-&gt;subtotal;
     
    // If Subtotal &lt; Min Amount Echo Notice
    // and add &quot;Continue Shopping&quot; button
     
    if ( $current &lt; $min_amount ) {
    $added_text = esc_html__(&#039;Get free shipping if you order &#039;, &#039;woocommerce&#039; ) . wc_price( $min_amount - $current ) . esc_html__(&#039; more!&#039;, &#039;woocommerce&#039; );
    $return_to = apply_filters( &#039;woocommerce_continue_shopping_redirect&#039;, wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( &#039;shop&#039; ) );
    $notice = sprintf( &#039;<a href="%s" rel="nofollow">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
    wc_print_notice( $notice, 'notice' );
    }
     
    }
    
    

    (What I did: removed on “}” that caused an error and replaced wc_price( $threshold by wc_price( $min_amount)

    Do you know why the cart notice is not shown on mobile?

    Best regards,
    Ricardo

    1. Thanks a million Ricardo 🙂 Re: cart notice, it could be a CSS problem?

  35. Great idea but it does not work when updating quantities on the actual product page.

    It remains static to the initial page load which defeats the purpose a bit for a user as they will want to see gradually how much more they have to buy as they tweak quantities.

    Personally not sure the functionality of the WC cart exists to manage that though.

    1. Hey Jimbo, thanks so much for your comment! This snippet works on the Cart page, so not sure what happens on the product page 🙂 Try disabling the Ajax add to cart maybe?

  36. Hi Rodolfo. This is a great snippet and works well (I mean the WooCommerce 3.0.5 tested version). The only bug I found is about the case where I add to cart a cross sell product in cart page. In this case the wc_price( $min_amount – $current ) remain the same and it is not updated until the page is refreshed. How to fix the bug? Thanks for your works

    1. Fabrizio, thanks for your comment! Without touching code, the fastest fix would be to disable the Ajax add to cart from the WooCommerce settings and force a page refresh 🙂

    2. Thanks Rodolfo…And…touching the code instead? 🙂

      1. Hey Fabrizio, yes that’s possible of course but it’s custom work and cannot help here via the blog comments 🙂

  37. Hi,
    I want to use a free shipping minimum order of 8.10, which is three products at 2.70 – so I am using this:

    $maximum = 8.10;
    	$current = WC()-&gt;cart-&gt;subtotal;
    	if ($current &lt; $maximum)
    		{
    		echo &#039;You will get free shipping if you spend £' . ($maximum - $current) . '.00 more!';
    		}
    

    And it works, except that in the cart page, if the customer has one product in the basket, the message is there but the figures read strangely – e.g. You will get free shipping if you spend £5.4.00 more!

    And I don’t know what to change to make it present OK.

    1. Hey David, can you test again with integer numbers only (8 and not 8.10)?

  38. Hi Rodolfo,
    Thank you very match for awesome snippet. But I have little problem.
    After pushing button ” back to the shop” I need redirect the visitor to root of shop page, not to the last position from which the client came to the cart page. will you please help me? thnkx

    1. Hello Peter, 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

  39. Is it possible to display this on a product page too? BTW very nice customization and the feedback from the community is great!

    1. Jonathan, 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 and nice words! ~R

  40. Hi Rodolfo,

    I love your snipets thanks a lot.
    I have problem with this one. I show the notification on 2 places on the cart page: 1 is “woocommerce_before_cart”, 2 is “woocommerce_proceed_to_checkout, 15” so above the button. Everything is showing up on the right place, the amounts are good. But: when I change quantity of a product and the cart refresshing the two notification “jumping” to the “woocommerce_before_cart_table” place. When I refresh the page itself everything is OK again. Do you have any idea what can coze this?

    1. Hey Brigitta, thanks so much for your comment! If you only enable the default “woocommerce_before_cart” notification, do you get this error?

    2. Hi,
      you mean the “cart is updated” message? That message is also on the “woocommerce_before_cart_table” place.

      1. Sorry – didn’t explain well. If you use my exact snippet, do you still get the error?

    3. yes. I just disabled the snipet, and tested. I have the same problem with that.
      WooCommerce version: 3.2.0
      WP version: 4.8.3

    4. Hi,

      Yep, I have the same problem with the original too.

      1. Ok Brigitta, I tested the snippet and I see no error on my end. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

        To troubleshoot, go to WP Dashboard > WooCommerce > System Status: what errors do you see in red font?

        Also, take a look at this tutorial to see how to troubleshoot: https://businessbloomer.com/woocommerce-troubleshooting-mistakes-to-avoid/

        Finally, can you try switching temporarily to “Twentyseventeen” or “Storefront” theme and let me know if it works?

        Hope this helps!

        R

    5. Hi Rodolfo,

      I watched the troubleshooting video, also a good one, congrats. I am learning a lot from you, many thanks!!
      I switched to “Twentyseventeen” : there I even did not get the notice.
      I disabled all my plugin except original woocommerce: the problem still there, the strange is that, if I comment out the snipet, the original “cart updated” message is also on the wrong place…
      System Status errors: – there are 6 plugins with: Not tested with the active version of WooCommerce
      – at Theme: 2 files: affinity/woocommerce/content-product.php verzió 2.6.1 old. new verzió: 3.0.0,
      affinity/woocommerce/global/quantity-input.php verzió 2.5.0 old. new verzió: 3.2.0.

      I tested the sinppet on another wp site with own theme, I copied there the cart.php coz I made a little modification on it and it is working perfectly.

      1. Ok, good to know – thank you 🙂

  41. Thanks for this Rodolfo.
    Does this allow me to apply the message to only the shipping zones that have free shipping? I only offer free shipping on domestic orders; the rest do not have free shipping. I don’t want this message to appear on orders that are not domestic.

    1. Tamara, thanks so much for your comment! No, this basic shipping doesn’t check what the currently selected/located shipping zone is before outputting the notice – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  42. Hi Rodolfo
    Cool snippet 😉
    Just wondering if you could provide me with a pointer please.
    In my woo setup I use advanced free shipping, 2 woo zones and 3 custom zones, and shipping by user role.
    Using your static version the snippet works great and displays accordingly.
    I am struggling to get the min amount for the specific method.
    I’ve been staring at this for two days now and could do with a direction to head in.
    Do I get the selected method first then try to get the min amount for that method, or do I get all methods and itterate through them to get min amount?
    And then… if is logged in conditional to retrieve the appropriate role rates.
    Maybe I am making this more difficult than it should be?
    Cheers

    1. Hey Andy, thanks so much for your comment! Yes, this is possible for sure and the code must be changed accordingly – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  43. Hi there,

    Sorry for the comment, I’m not an expert
    I added the sniped (dinamic first and then tried with the static) to the functions on my child theme. The first one didn’t show anything and the second one broke the site (error 500).

    Any idea what could it be or what I’m doing wrong?

    Thanks

    1. Monica, thanks for your comment! What error did you get (enable WP_DEBUG and you will get a description of the error)?

  44. Hello,
    is there a way i change the style text for “Get free shipping if you order ….” like text color or text size ?

    Thank you

    1. Albert, thanks for your comment! Yes, of course, you will need custom CSS for that. Sorry but I can’t help with custom work via the blog comments – thanks for your understanding!

  45. Hi,

    Great articld, may ı ask you something about it ?
    İs there any way to setting minimum order amount for different zones ?
    For example
    Minimum order amount should be 100 usd for zone code 34234 and it should be 50 usd for 41234

    ı am looking for it since couple weeks

    Thanks in Advance

    1. Chad, thanks for your comment! Each shipping zone has its own Free Shipping method; each Free Shipping method has its own threshold. If you do things right in the settings, this snippet will automatically know the current zone threshold. R

  46. hello sir,

    I just wanted to know if this works as well if the cart has shipping fee of say $10, then when a customer reached $250, the cart will then say FREE shipping, else the shipping fee of $10 will apply. I really appreciate your help

    1. Hey Zevi, 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

  47. hi Sir,

    We would like to use this snippet.

    Unfortunately, every example does not have the correct ‘Sum’ of the prices.

    Maybe it is because of WC 3.0.8?
    Which snippet do we need to use for this version?

    Thanks in advance

    1. Hey Kuul, thanks for your comment! This snippet is compatible with Woo 3.0 and above, so I’d be surprised if that’s the issue. Probably the sums are not correct as you have taxes/shipping/fees etc., so it will need to be customized to your specific settings. Sorry but I can’t help here in the blog comments for free 🙂

    2. Hey Kuul – You should just be able to change to be a different class that’s being used elsewhere in your theme and which doesn’t have the fancy flash in and out attributes.

  48. This is awesome, thanks. Any ideas on how I might add this info to the page itself (rather than wrap it in a notice)? My theme has rewritten the standard woo notice with custom flashing javascript notices that flash in and out, and reformats this message in a strange way ~ I’d love to just have this notice above the cart info, for example.

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

  49. Hi Rodolfo,
    I added the code into my functions.php of my child theme but receive this error:

    Notice: Undefined variable: min_amounts in….child/functions.php on line 79

    Line 79 has this code on it – if ( is_array($min_amounts) ) {

    Where do I need to define the min amount?

    1. Hey Brett, thanks for your comment! Uhm, silly question I know, but do you have any “Free Shipping” methods with a “minimum order amount”?

      1. Sure do Rodolfo, free shipping on orders over $500.

        1. 🙂

          And to what zone is that linked to?

          1. I’ve named the zone “Australia Wide”

            And the zone region is “Australia”

            1. Gotcha 🙂 And I guess under WooCommerce > Settings you have “Geolocate” as the customer’s default address?

              1. Yes, that’s correct.

                1. Yes, I see. I’m testing it and if your geolocation is not the same as the zone with free shipping it doesn’t get the minimum amount 🙁 I’ll see what I can do!

                2. Cool – I think I fixed it now. I rewrote the whole function and found a few bugs as well, so delete previous and copy the new one 🙂 Let me know!

                  1. GREAT!
                    Thanks Rodolfo, works like a charm.

  50. Hello Rodolfo,
    I tried this code locally, but after reloading the page the message disappears. Why is this happening?

    1. Hey Aleksandar, thanks for your comment! I spent some time revising, retesting and correcting this snippet, now it should work – let me know 🙂

  51. Hi, I trid to use this code, so I put it to functions.php of my chield theme. But there is no text/alert that I have to add more things for free shipping, It simply just dont work. I have the latest WP installed. What to do?

    1. Hey Vv, thanks for your comment! Can you try with the “Static” snippet I just added to the blog and then let me know?

  52. Thank you so much for this code & tutorial.

    How would I set it up to NOT INCLUDE TAX in the calculation? If my free shipping starts at $30 and I have $14 in the cart, I would like it to say I only need $16 more to get free shipping.

    I’m also having trouble with CSS to make the button “continue shopping” … I can probably figure it out eventually, but if you have your CSS for the bar and button available, that would be wonderful if you could share it.

    Thanks!

    1. Thanks for your comment Bob! Try using get_total_ex_tax() instead of subtotal (https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html). Let me know!

      1. Thanks for quick reply! (BTW – just signed up as a Supporter)…

        I don’t know if I used your code correctly, but now it returns “0”. Below is how I used the above code. (To keep it simple, I set minimum amount to $30.)

        // Set Min Amount
        $min_amount = 30;

        // Get Cart Subtotal inc. Tax excl. Shipping
        $current = WC()->cart->get_total_ex_tax();

        1. Uhm, weird. Maybe:

          WC()->cart->get_cart_total();

          1. Nope, didn’t work. Still returns “0”.

            I may give up for now and just have bar say, “Free Shipping if you order $30 or more.” and forget trying to calculate $30 – [subtotal without tax] = amount needed to purchase to get free shipping.

            Thanks for trying.

  53. Hi, nice feature and easy to implement but i have a question :

    Is there any change to make it permanent when u go to cart ?
    Or i have a template issue, because i only see it when i update cart…

    And mini cart seems won’t be able to display this messagen we talk about widget from woocommerce ?

    Regards

    1. Hey LeSScro, thanks for your comment! What do you mean by “permanent”? Let me know!

  54. Hello,
    thanks for this great tip! I have an issue.
    If i navigate from checkout page to the cart page, then the message shows correctly.
    If i navigate from another page to cart page, the message doesn’t appear. It seems that shipping methods are not loaded.
    I run Woocommerce 2.6.11 on WordPress 4.7.
    Thanks for your support!

    1. Hey Tiz thanks for your comment! I believe this snippet only works when the “system” knows the shipping zone (and as long as that shipping zone has a free shipping method). If not, no message shows I believe.

      Question – do you have just one free shipping method? In this case we can avoid to “get” the shipping zone and just hardcode the – for example = $150 into the function. Not sure if this makes sense?

  55. Hey Rodolfo! Excellent code. Exactly what I was looking for and didn’t have to buy a bulky plugin.

    2 questions to help me finish it off:

    1. When I change currency, the figures are calculated from 100. I don’t have this number hardcoded. I use free shipping at $100 AUD. But how can it change when the currency selection is made? It needs to be calculated from the currency equivalent of $100 AUD, not just 100.
    2. Can you suggest some more hooks to add the message:
    A. Directly below the “Proceed to Cart” button
    B. Top of the Checkout page
    C. Bottom of Checkout page
    D. Directly below the “Proceed to Paypal” button on the checkout page.

    Thanks again! I’d be happy to buy you a beer or coffee. Let me know where I can donate!
    Rob

    1. Hey Rob thanks for your comment and further message! As I said, this snippet doesn’t work with multicurrency, and will need an edit also based on the currency switcher plugin you use. In regard to the second question, I suggest to take a look at the available hooks on the cart (https://businessbloomer.com/woocommerce-visual-hook-guide-cart-page/) and checkout page (https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/) to have an idea of where you can accomplish that. Hope this helps!

  56. Is it possible to make this line:

    if ( $current < $min_amount ) {
    echo 'Continue ShoppingGet free shipping if you order ‘ . wc_price( $min_amount – $current ) . ‘ more!’;
    }

    WPML ready? I’ve tried with _e(‘text’,’domaintext’) but it doesn’t work.

    1. Hey Duke, thanks so much for your comment! Yes, of course it is, I don’t see why it shouldn’t work – just take a look at how WPML suggests to echo strings and you’ll probably figure it out. Something along the lines of (untested):

      [php]
      echo ‘‘ . _e(‘Continue Shopping’,’woocommerce’) . ‘‘ . ‘Get free shipping if you order ‘ . wc_price( $min_amount – $current ) . ‘ more!’;
      [./php]

  57. HI Rodolfo

    I have used your snippet and have been very glad for it. Thank yo so much.
    I have one questions regarding this – before we set this to 500 as a minimum buy to get free shipping – works great – but know we set this to 300 – but it still keeps saying 500 for free shipping.

    I have triple checked everything in the shipping settings and for the plugin we are using to show drop points for a package, it gives the free shipping at 300 as it should, but the snippet still tells that you need 200 more to get free shipping. I think it is something from the old shipping settings, that the snippet is taking the info from. Is there a way that I can adjust the amount inside the snippet? just tell the snippet to set it to 300?

    Best regards

    Michael

    1. Michael, thanks for your comment! Yes, you can just tell the snippet to use ‘300’ instead of reading it from the settings.

      Substitute this:

      // Get Min Amount from Woo Settings
      $free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
      $min_amount = $free_shipping_settings['min_amount']; 
      

      With this:

      // Set Min Amount 
      $min_amount = 300; 
      

      Let me know if this works 🙂

      1. Hi Rodolfo

        Thank you very much – It works like a charme 🙂

        But I just set it in the mini cart/widget cart

        Where do I put this in for the cart page? I dont see:

        // Get Min Amount from Woo Settings
        $free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
        $min_amount = $free_shipping_settings['min_amount']; 
        

        Anywhere in the snippet?

        Should I delete all of this:

        global $woocommerce;
        $shipping_methods = $woocommerce-&gt;shipping-&gt;get_shipping_methods();
         
        // Loop through the array to find min_amount value/s
         
        foreach($shipping_methods as $key =&gt; $value) {
            if ( $shipping_methods[$key]-&gt;min_amount &gt; 0 ) {
              $min_amounts[$key] = $shipping_methods[$key]-&gt;min_amount;
            }
        }
         
        if ( is_array($min_amounts) ) {
         
        // Find lowest min_amount
         
        $min_amount = min($min_amounts);
        

        With this:

        // Set Min Amount 
        $min_amount = 300; 
        

        Again thank you so much – I am following you for upcoming events/snippets 🙂

        best regards

        Michael

        Ps. Thank you for your quick response.

        1. Hey Michael, uhm, yes, sorry I was looking at the wrong snippet! Yes, you’re 100% correct. Let me know if it works 🙂

          1. Hi Rodolfo

            Unfortunately it do not work – it do not show anything at all 🙁

            I delete what mentioned in my other message and insert as you have shown me, but nothing happens.

            Could you try man setup a finished snippet, the way it should look, so I dont delete to much of it?

            Thank you again.

            1. Sure thing:

              
              function bbloomer_free_shipping_cart_notice_zones_new() {
              
              $min_amount = 300;
              
              $current = WC()->cart->subtotal;
              
              if ( $current < $min_amount ) {
              echo '<div class="woocommerce-message"><a href="' . get_permalink( woocommerce_get_page_id( 'shop' ) ) . '" class="button wc-forward">Continue Shopping</a>Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!</div>';
              }
              
              }
              
              add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice_zones_new' );
              
              
              1. I cant thank you enough Rodolfo – Works like a charm 🙂

                best regards

                Michael

  58. Maybe i found some kind of bug:
    your code works great but if in the cart page you hit refresh or f5 the page reload and the value of remaining amount for free shipping “mysteriously” increase (in my case the total for free shipping mysteriously become 154€).
    If you exceed the minimum of order for free shipping or empty the cart then all value back to normal.
    I tested on a site the 2.6+ version and it always occurs.
    It’s a real bug or my mistake?
    can you verify?
    Thanks a lot
    PS: sorry for my english i’m italian (maybe you too?)

    1. Ok, thank you Andrea – and your English is perfect (yes, I’m Italian). I will do some testing and then if necessary post a revised snippet 🙂

  59. This doesn’t show until I change the quantity of products in the cart and then update, when I refresh the page it disappear again.

    If I also clear transients and refresh the page it shows, after the second refresh it disappear again.

    Any ideas?

    1. Sam, thanks for your feedback! Uhm, I understand the “echo” part is not ideal, I should have used the wc_notice function instead. Could you possibly double check by disabling ALL plugins but WooCommerce and switching theme temporarily to 2016, and let me know if it gives you the same error? Cheers 🙂

      1. Hello Rodolfo,

        I got the same error that the man above me. I need to refresh it or go to checkout and back to view cart so it could appear.

        Any fix is more than welcome, and yes, I could prove you WP-admin details to test 🙂

        Thank you so much!

        1. Drake, I tested this on my site and I don’t need to refresh anything to make it appear: https://screencast.com/t/ThdgfpMJEy

          Sure that you have the correct shipping settings? Sure, feel free to send me your login (privately) and I’ll take a look.

      2. Sam, I retested this on my site and I don’t need to refresh anything to make it appear: https://screencast.com/t/ThdgfpMJEy

        Sure that you have the correct shipping settings? Can you send me a screenshot/screencast with those?

        1. Hello Rodolfo,

          I had the same issue. The legend only appeared when I added a product to the cart and then disappeared after page reload. I did some experimentation. At some point, I activated the “Enable Shipping Debug Mode to show matching shipping zones and to bypass shipping rate cache” option at “WooCommerce/System Status/Tools” and now the legend always appears normally.

          Unfortunately, an additional unwanted legend appears which shows the shipping zone. I have managed to remove this additional legend. However, I wonder if activating the “Enable Shipping Debug Mode to show matching shipping zones and to bypass shipping rate cache” option is a good thing on the long run.

          1. Hey, thanks for your comment 🙂 No, the shipping debug is not a good idea. I’ll do some tests and in case release a new update. Thanks for your patience 🙂

  60. Hello Rodolfo

    Is there a way to hide the info if the minimum amount is reached?

    1. Michael, thanks for your comment! Actually, the line:

      if ( $current < $min_amount ) {
      

      …already does what you’re asking: unless the total is lower than the minimum, the notice will not be displayed. Unless you’ve tested this and are getting a different behavior? Let me know 🙂

      1. Thank you it works now. I had a little bug in my script.

        1. Excellent! Thank you 🙂

  61. Thanks for so great code snippet! Is it possible to add one more condition according to item quantity? For example, a customer can reach either amount or quantity to get free shipping. Thanks again!

    1. Hey Amos, thanks for your comment and great feedback. Please note – this snippet creates a notice; it does not change your shipping settings. WooCommerce shipping does not allow you to award free shipping based on quantity, so you will need to find a relative plugin/snippet for that. Once this is active, then you can customize this notice, but it’s 100% dependent on the plugin/snippet you select. Hope this makes sense 🙂

      1. Thanks for your answer. I understand now. 🙂

        1. No problem Amos, you’re welcome 🙂

  62. Great Tips!! Just quick question. I’m using latest woocommerce and using Zone inside free shipping not the ‘woocommerce_free_shipping_settings’.
    Do you guys know the Zone’s inside free shipping?
    Thanks

    1. Hey Subin, thanks for your comment. I’m currently working on releasing an update to this snippet which would be compatible with Woo 2.6+. Hope to publish this soon 🙂

      1. 2.6 compatible snippet has now been published 🙂

  63. Does this also work with woocommerce 2.6+? I tried it out but it doesn’t work. I’m not sure that the problem is.

    1. Michael, thanks for your comment! Yes, indeed, you’re correct 🙂 I’ll see if I have some time today to publish the updated version. Thanks a lot!

      1. That would be great. Thank you!

        1. 2.6 compatible snippet has now been published 🙂

          1. Thank you very much. I implemented the snippet to my site. It works. But if I refresh the cart i get the following warning.

            Warning: min(): When only one parameter is given, it must be an array in /home/fsbswitz/public_html/subdomain/sidefyn/page/wp-content/themes/betheme_child/functions.php on line 98

            Line 98 is “$min_amount = min($min_amounts);” of your snippet.

            Do you have an idea to fix this?

            1. Michael, thanks so much for your comment! You’re right 🙂 I just revised the snippet and wrapped the last part within a conditional check:

              if ( is_array($min_amounts) ) {
              ...
              }
              

              Let me know if this works!

              1. Thank you! It works.
                I’m looking for a solution to show this snippet also to not logged in users. Someone knows how to do that?

                I made the output multiligual. If some one need it…. I did it for French and German.

                /**
                * @snippet Notice with $$$ remaining to Free Shipping @ WooCommerce Cart
                * @how-to Get CustomizeWoo.com FREE
                * @sourcecode https://businessbloomer.com/?p=442
                * @author Rodolfo Melogli
                * @testedwith WooCommerce 2.6.4
                */
                
                function bbloomer_free_shipping_cart_notice_zones() {
                
                // Get Shipping Methods for Current Zone
                
                global $woocommerce;
                $shipping_methods = $woocommerce->shipping->get_shipping_methods();
                
                // Loop through the array to find min_amount value/s
                
                foreach($shipping_methods as $key => $value) {
                    if ( $shipping_methods[$key]->min_amount > 0 ) {
                      $min_amounts[$key] = $shipping_methods[$key]->min_amount;
                    }
                }
                if ( is_array($min_amounts) ) {
                // Find lowest min_amount
                
                $min_amount = min($min_amounts);
                
                // Get Cart Subtotal inc. Tax excl. Shipping
                
                $current = WC()->cart->subtotal;
                
                // If Subtotal < Min Amount Echo Notice
                // and add "Continue Shopping" button
                
                if (ICL_LANGUAGE_CODE == 'fr')  {if ( $current < $min_amounts ) {
                echo '<div class="woocommerce-error"><a href="' . get_permalink( woocommerce_get_page_id( 'shop' ) ) . '" class="button wc-forward">Continuer vos achats</a>La livraison est gratuite si vous commandez ' . wc_price( $min_amount - $current ) . ' plus!</div>';
                }   } else  {if ( $current < $min_amounts ) {
                echo '<div class="woocommerce-error"><a href="' . get_permalink( woocommerce_get_page_id( 'shop' ) ) . '" class="button wc-forward">Weiter einkaufen</a>Es fehlen noch ' . wc_price( $min_amount - $current ) . ' bis zum kostenlosen Versand!</div>';
                }   }
                 }
                }
                
                add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice_zones' );
                1. Hey Michael, thanks for following up and sharing your WPML-friendly snippet version 🙂 In regard to showing this to not logged in users – well, this already shows to not logged in users! Did you mean to “showing this to logged in users only” maybe?

                  1. Hmm it is maybe something other. But I don’t see it if i’m not logged in

                    1. In the WooCommerce settings, do you have a default shipping address or geolocation set up? Otherwise the system doesn’t know what country/zone you’re in once you’re on the Cart. Try this and let me know 🙂

                    2. You are the best!! That was it!

  64. Is it also possible to do something like this:

    – “You Only Need to Spend $$ to Get $4 discount”

    – “You Only Need to Spend $$ to Get $7 discount”

    – “You Only Need to Spend $$ to Get $10 discount”

    Thanks

    1. Hello Julian, thanks for your comment! Well, yes, I don’t see why not. You just need to tweak the line that starts with “echo”, and customize that according to your needs. Let me know how it goes!

  65. Sorry for the noob question, but where do I add these snippets?

    1. Fred, thanks for your comment – and no question is noob whatsoever 🙂 You can place this in your child theme’s functions.php file – if you need more guidance, please take a look at this video tutorial: “How to Customize WooCommerce with PHP Snippets (even if you don’t know PHP)“. Hope this helps!

  66. Works great! Been looking for this for a while. Thank you!

  67. Great snippet Rofoldo!

    I wanted to use it on my checkout page rather than as a cart notice, so I made it into a shortcode (called “shipping35”. In case anyone else wants it, here is the code:

    
    // If your free shipping threshold is over $XX as per Woocommerce settings...
    
    function shipping35() {
    	$maximum = 35;
    	$current = WC()->cart->subtotal;
    
    // Format the difference as per Woo currency formatting settings
    	$formatteddifference = wc_price( $maximum - $current );
    		if ( $current < $maximum ) {
    		return 'Get free international shipping on all orders over $35 USD!</br></br>For free shipping on this order, just add <span style="font-weight: bold; font-size: 20px;">' . $formatteddifference . '</span> more! ';
    }
    		else {
    		return 'Great! You have FREE shipping on this order!';
    }
    }
    add_shortcode( 'shipping35', 'shipping35' );
    
    

    Hope that comes in useful for someone!

    Charlie

    1. Thank you very very very much Charlie!

    2. Hello Charlie!
      I have used your code on several sites, and it works very well, but recently one of our customers noticed an issue, when a coupon is used that makes the subtotal go below the amount required for free shipping, it still displays that free shipping is available.

      Is there any easy way to fix this?

      1. Thanks Mathias! I guess you’ll need to tweak the variable $current to account for that – hope this helps a little 🙂

  68. Ciao Rodolfo,

    thanks for the useful woo-tips, always helpful.

    I modified the code so that the free shipping message displays the cart subtotal AFTER a coupon discount is applied, so it matches the correct threshold:

     $current = WC()->cart->cart_contents_total; 

    So changing “subtotal” to “cart_contents_total” seems to do the trick for me and the subtotal is calculated correctly with discount.

    Grazie!

    1. Gigio, thanks so much for your message and suggestion 🙂 I’m sure many readers will be thankful too! Grazie 🙂

  69. I would like to display this in the cart widget too – can it be done?

    Love the function!

    1. Linda, thanks for your feedback! And YES! You can do so by using the hook: “woocommerce_before_mini_cart”. I’m updating the code in a few minutes. Rodolfo

      1. I added this code and it seems to work – not sure it is the best way to do it:

        add_action( ‘woocommerce_before_checkout_billing_form’, ‘cart_notice’ );
        add_action( ‘woocommerce_widget_shopping_cart_before_buttons’, ‘cart_notice’ );

        1. Awesome Linda, thanks so much!

  70. Good day!

    Trying to get this to work with GPB but struggling. We have the thousand and decimal separator settings set in Woocommerce but the price is showing £2900.4 as we have ours set to £3000 minimum order delivery. Would ideally like this to show as £2,900.40 🙂

    Great snippet btw!

    Thanks 🙂

    1. Hey thanks for your feedback! Try using wc_price($maximum – $current) and let me know if this does the trick!

      1. Thank you for the quick response 🙂
        It resulted in Parse error: syntax error, unexpected ‘–’ (T_STRING)

        1. Here’s the working snippet:

          function cart_notice() {
          $maximum = 1500;
          $current = WC()->cart->subtotal;
          $formatteddifference = wc_price( $maximum - $current );
          if ( $current < $maximum ) {
          echo '<div class="woocommerce-message">Get free shipping if you order ' . $formatteddifference . ' more!</div>';
          }
          }
          add_action( 'woocommerce_before_cart', 'cart_notice' );
          

          Hope this helps 🙂

      2. Thanks for the quick response 🙂
        Still spitting out the “Parse error: syntax error, unexpected ‘–’ (T_STRING)” so not sure if it is the way my server is configured but thanks anyway 😀

        1. Yes, it seems you used some character that is not recognised by your system or forgot a “;” somewhere 🙂

  71. If you don’t want to hardcode the maximum amount, you could fetch it from WooCommerce settings:

    https://gist.github.com/jukra/65373cde1d54770670e8

    1. Jukka, thanks so much for your valuable feedback!

  72. Thanks, that worked beautifully!

  73. Worked great. Is there a way to check to see if free shipping is available first? We run promos for a month that has the free shipping so basically we want it to check if free shipping is available as an option and if it is then display the message. That way we don’t have to comment out the code, we can just turn off the free shipping option in the settings.

    Also, is there a way in WC to pull the minimum dollar amount for the free shipping to apply rather than hard coding it?

    1. Travis, thank you for your feedback.

      Yes, of course you can check if “free shipping is available”! Take a look at our post and use a similar PHP conditional to only show the message if free shipping is not available.

      In relation to the other question, that’s another “of course”. Just go to Woocommerce / Settings / Shipping / Free Shipping and beside “Free Shipping Requires..” choose a minimum amount, then enter such amount in the field below.

      Hope this helps 🙂

  74. Hi, is it possible to display this notice only when there actualy IS shipping cost? And hide it for downloadables and virtual?

    1. Hi Michal thanks for your message! Yes, I guess you could check if the cart contains virtual products, and if yes, decide not to show the notice. This is the PHP you can use to detect if there is a virtual product in the cart (source: https://www.remicorson.com/woocommerce-hide-checkout-fields-for-virtual-products):

      
        // By default, no virtual product
        $has_virtual_products = false;
        
        // Default virtual products number
        $virtual_products = 0;
        
        // Get all products in cart
        $products = $woocommerce-&gt;cart-&gt;get_cart();
        
        // Loop through cart products
        foreach( $products as $product ) {
      	  
      	  // Get product ID and '_virtual' post meta
      	  $product_id = $product['product_id'];
      	  $is_virtual = get_post_meta( $product_id, '_virtual', true );
      	  
      	  // Update $has_virtual_product if product is virtual
      	  if( $is_virtual == 'yes' )
        		$virtual_products += 1;
        }
        
        if( count($products) == $virtual_products )
        	$has_virtual_products = true;
        
        return $has_virtual_products;
      
      
      1. I tried to glue it myself – with no luck 😉

    2. I managed to make it work, this way if it has only virtual products there is no notice, every other case there is.

      /**
      * @snippet Notice with $$$ remaining to Free Shipping @ WooCommerce Cart
      * @how-to Get CustomizeWoo.com FREE
      * @sourcecode https://businessbloomer.com/?p=442
      * @author Rodolfo Melogli
      * @testedwith WooCommerce 3.4.2
      */
       
      function bbloomer_free_shipping_cart_notice_zones_new() {
      	
      	 if( woo_cart_has_virtual_product() == false ) {
      		 $min_amount = 50000;
       
      		$current = WC()->cart->subtotal;
       
      		if ( $current < $min_amount ) {
      		echo '<div class="woocommerce-message"><a href="' . get_permalink( woocommerce_get_page_id( 'shop' ) ) . '" class="button wc-forward">Vásárlás folytatása</a>Még ' . wc_price( $min_amount - $current ) . ' az ingyenes szállításhoz!</div>';
      		}
      	}
       
      }
      
      /**
       * Check if the cart contains virtual product
       *
       * @return bool
      */
      function woo_cart_has_virtual_product() {
        
        global $woocommerce;
        
        // By default, no virtual product
        $has_virtual_products = false;
        
        // Default virtual products number
        $virtual_products = 0;
        
        // Get all products in cart
        $products = $woocommerce->cart->get_cart();
        
        // Loop through cart products
        foreach( $products as $product ) {
      	  
      	  // Get product ID and '_virtual' post meta
      	  $product_id = $product['product_id'];
      	  $is_virtual = get_post_meta( $product_id, '_virtual', true );
      	  
      	  // Update $has_virtual_product if product is virtual
      	  if( $is_virtual == 'yes' )
        		$virtual_products += 1;
        }
        
        if( count($products) == $virtual_products )
        	$has_virtual_products = true;
        
        return $has_virtual_products;
      }
       
       
       
      add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice_zones_new' );
      add_action( 'woocommerce_before_checkout_form', 'bbloomer_free_shipping_cart_notice_zones_new' );
      
      1. Nice!

  75. oohhh man, i just found that my theme is doing something wrong with that, just tested the code on another site and it work like a charm…

    1. Great! Glad to hear that 🙂

  76. good day, im trying the code but it didnt work, could u maybe help me what im doing wrong??
    im putting that code on my code/functions.php file.

    // If your free shipping threshold is 1500 as per Woocommerce settings…
    // …show a cart notice if order subtotal is less than 1500

    function cart_notice() {
    $maximum = 600;
    $current = WC()->cart->subtotal;
    if ( $current < $maximum ) {
    echo 'Recibe tu Envio Gratis $’ . ($maximum – $current) . ‘ mas!’;
    }
    }

    add_action( ‘woocommerce_before_cart’, ‘cart_notice’ );

    1. Renato, thanks for your comment! What version of Woocommerce are you on?

      1. hello, i been tried on the next 2 versions:
        WC: 2.1.8
        WC: 2.1.9

        1. I see. Try using this instead:

          
          function cart_notice() {
          global $woocommerce;
          $maximum = 600;
          $current = $woocommerce-&gt;cart-&gt;get_cart_subtotal();
          if ( $current &lt; $maximum ) {
          echo 'Recibe tu Envio Gratis $’ . ($maximum – $current) . ‘ mas!’;
          }
          }
          
          add_action( ‘woocommerce_before_cart’, ‘cart_notice’ );
          
          
          1. well, just tried the code and didnt work. added the “” because make an error but it didnt show anything. updated my Woocommerce to the version 2.2.6 and still doesnt work.

  77. wow, thats fine! and simple!

    the german text snippet is

    “Nur noch ‘ . ($maximum – $current) . ‘ € bis zur kostenlosen Lieferung!”

    so i have the € after the difference price and a space between both.

    but two little problems:
    1: i need a comma between euros and cents, not a dot,
    2 and i need two digits after the comma

    is it possible top fix this?

    and an idea for the pro version – if you will do this to a plugin – it would be nice when it use the free delivery price from the woo settings. 😉

    thanks and greetz .-)

    1. Reini, thanks for your comment. When I use this snippet on my test website, it gives me: “Get free shipping if you order $1498.98 more!”, so it correctly adds two decimals and the “dot”. Therefore, if you have this set up correctly under Woocommerce > Settings > General > Currency Options, it should do what you’re asking!

      1. thanks for your fast repley 🙂

        the woocommerce settings with the comma, the two digits and the € after with space ist the first step after installing woocommerce 🙂

        i have a testshop too, the same problem, no digits for the cent (only prices without cents).
        in our not-test-shop now i have three digits, i think it is because the tax, it grabs the tax before rounding.

        1. Try this edit (see above function):

          if ( $current &lt; $maximum ) {
          setlocale(LC_MONETARY, 'de_DE');
          echo '&lt;div class=&quot;woocommerce-message&quot;&gt;Get free shipping if you order ' . money_format('%=*^-14#8.2i', ($maximum - $current)) . ' more!&lt;/div&gt;';
          }
          

          Reference: https://php.net/manual/en/function.money-format.php

          1. good morning and thank you, now the comma and the number of digits are ok 🙂

            but i have 6 stars before the price -> screenshot: https://dl.dropboxusercontent.com/u/2455851/campbloomer/free-shipping-01.png

            the other thing is, that the snippet grabs the absolut summary including tax and delivery, right? i have a B2B-shop, so i need the difference between only the products summary – whitout tax and delivery – and the free delivery value.

            i think for B2C shops are also is the price without the delivery the right point. i hope you can understand me 🙂

          2. oh, and i see “EUR €” now …

          3. ok, the stars are placeholders. when i delete the star in the string between “=” and “^” and set a space, the its ok.

            money_format(‘%= ^-14#8.2i’,

            🙂

          4. sorry for spamming 😉

            deleting the de_DE string is deleting the EUR, but it sets the comma to a dot back.

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 *