Who doesn’t love FREE stuff?

Offering free shipping is something that might just prompt the user to make that one extra purchase. For small products, the cost increase is minimal at most, and it’s always better to sell more, right? WooCommerce has the ability to set up free shipping, but you may want to incitivise purchased by only offering it when there’s a certain amount in the cart.

This snippet will add a notification to the top of the cart page to notify the user of how much more they need to put in the cart in order to qualify for free shipping.

Before you just drop this snippet into place, you’ll need to set up a Free Shipping option in the Woocommerce > Shipping menu.

Here’s how you do that:

  1. Create a shipping zone
  2. Select the zone and click “Add shipping method”
  3. Enter your shipping details.

Screenshot of WooCommerce Shipping Setting

The simple notification snippet

/**
 * @snippet       $$$ remaining to Free Shipping @ WooCommerce Cart
 * @compatible    WooCommerce 3.9
 */

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

Bonus Snippet to exclude specific user roles from free shipping

This is the same callback function as above but doesn’t trigger the action if the user is logged in with this user role.

/**
 * @snippet       $$$ remaining to Free Shipping @ WooCommerce Cart
 * @compatible    WooCommerce 3.9
 */

if( is_user_logged_in() ) {
	$user  = wp_get_current_user();
	$roles = ( array ) $user->roles;   

	// don't show the message if logged in as a wholesaler
	if ( !in_array( 'wholesale_customer', $roles ) && ! in_array( 'wholesale_customer_e', $roles ) ) {
		add_action( 'woocommerce_before_cart', 'jrc_free_shipping_cart_notice' );
	}
} else {
	add_action( 'woocommerce_before_cart', 'jrc_free_shipping_cart_notice' );
}
  
function jrc_free_shipping_cart_notice() {
  
   $min_amount = 250; //change this to your free shipping threshold
   
   $current = WC()->cart->subtotal;
  
   if ( $current < $min_amount ) {
      $added_text = '<div>Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!</div>';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<div class="shipping-cart-notice"><a href="%s" class="button wc-forward">%s</a> %s</div>', esc_url( $return_to ), 'Continue Shopping', $added_text );
      wc_print_notice( $notice, 'notice' );
   }
}

WooCommerce is by far the best way to sell products on your WordPress site, but sometimes it needs a little help to do the specific things you want for your store. Hopefully this snippet helps resolve one of those issues without having to install a plugin.