• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
SnippetPress

SnippetPress

The Home of WordPress Snippets

Vultr Web Hosting
  • Cheat Sheets
  • Reviews
  • Snippets
  • Tutorials

WooCommerce – Display an “Add $X To Get Free Shipping” Notice

July 13, 2020 by SnippetPress Leave a Comment

A great way to get customers to purchase more and increase your average order value is to offer free shipping above a certain amount. Letting your customers know how close they are to getting free shipping on the cart and checkout page can then prompt them to spend more. Let’s take a look at how to do this.

We’re going to assume that you have already set up a free shipping option in your WooCommerce settings, and that this option is only available for orders above a certain amount. If that’s the case, then all you need to do is use the following code snippet to display the free shipping notice on the cart and checkout page:

function snippetpress_free_shipping_notice() {

	if ( ! is_cart() && ! is_checkout() ) {
		return;
	}

	$packages = WC()->cart->get_shipping_packages();
	$package = reset( $packages );
	$zone = wc_get_shipping_zone( $package );

	$cart_total = WC()->cart->get_displayed_subtotal();
	if ( WC()->cart->display_prices_including_tax() ) {
		$cart_total = round( $cart_total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
	} else {
		$cart_total = round( $cart_total - WC()->cart->get_discount_total(), wc_get_price_decimals() );
	}
	foreach ( $zone->get_shipping_methods( true ) as $i => $method ) {
		$min_amount = $method->get_option( 'min_amount' );


		if ( $method->id == 'free_shipping' &amp;&amp; ! empty( $min_amount ) &amp;&amp; $cart_total < $min_amount ) {
			$remaining = $min_amount - $cart_total;
			wc_add_notice( sprintf( 'Add %s more to your cart to get free shipping!', wc_price( $remaining ) ) );
		}
	}

}
add_action( 'wp', 'snippetpress_free_shipping_notice' );

If you just want the notice to display on the cart and not on the checkout, remove && ! is_checkout() from the second line in the snippet.

Now when customers view their cart or checkout, they will see a notice telling them how much more they need to spend to get free shipping, and this will hopefully get them spending more in your store!

Where to add the snippet?

The snippet should be placed at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.

Filed Under: Snippets, Snippets, WooCommerce

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

E-mail Newsletter

More to See

WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart

July 15, 2020 By SnippetPress

WooCommerce – Display an “Add $X To Get Free Shipping” Notice

July 13, 2020 By SnippetPress

Footer

Recent

  • Change The WordPress Page Title Separator Without A Plugin
  • WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart
  • WooCommerce – Display an “Add $X To Get Free Shipping” Notice
  • WooCommerce – Integrate Google Merchant Center Customer Reviews
  • WooCommerce – Disable Out of Stock Variations

Search

Copyright © 2022 ·SnippetPress