Skip to content

Simple Conditional Shipping Notice In WooCommerce

My wife is working on an online cabinet company that we hope to launch any day now. Knowing that free shipping is a great draw for sales, we wanted to offer that to our customers, but only if they spent $2,500 in a single order. We could have used the excellent Cart Notices extension, but that seemed like a bit much since I only need one notice.

So I decided I’d write a quick conditional statement that lets customers know how much more they have to add to the cart to get free shipping or lets them know their order qualifies already if they have enough in their cart.

The first steps in writing the code for a conditional statement are to determine what the conditions are and what happens if they are met.

  • If a customer has less than $2,500 in their cart, give a notice encouraging them to add more.
  • If a customer has $2,500 or more in their cart, give a different notice congratulating them on getting free shipping.

Let’s translate this into code for WooCommerce. Here’s the first section of our code block.

global $woocommerce;
$total = WC()->cart->get_cart_contents_total();
$threshold = 2500;
$balance =  $threshold - $total;
$formatted_balance = number_format($balance, 2, '.', ',');
	
$add_more = 'Add $'.$formatted_balance.' more for free shipping!';

What’s happening here is we’re loading up the value of the items in our cart and storing them in a variable called $total. Next, I’ve set a variable called $threshold to hold the amount users need to purchase for free shipping. In this case, 2500.

To find out where the cart stands in relation to the threshold, we do a bit of math and subtract the total from the threshold and store it in $balance. Since I will be displaying the balance to users who need to add more to the cart, I run it through PHP’s number_format() so it is formatted as I’d like it to be.

Lastly, for this section, I pull this all together into a statement I can display when the conditions have been met. To actually make this display, we’ll use WooCommerce’s wc_add_notice() function. Now we need to run this information through a conditional statement. Here’s how we do that.

if ( $balance < 0 ) {
    wc_add_notice( 'You qualify for free shipping!', 'success' );
} else {
    wc_add_notice( $add_more, 'notice' );
}	

This is a simple if/else statement. We state our condition at the top, in this case, if the $balance is less than 0 then that means our cart qualifies for free shipping. Otherwise, we need to show our add more notice to the customer.

So how do we show this to the customer? We wrap it all into a function like so:

function ijab_free_shipping_cart_notice() {
    if ( is_admin() ) return;
    
    global $woocommerce;
    $total = WC()->cart->get_cart_contents_total();
	$threshold = 2500;
    $balance =  $threshold - $total;
	$formatted_balance = number_format($balance, 2, '.', ',');
	
	$add_more = 'Add $'.$formatted_balance.' more for free shipping!';

    if ( $balance < 0 ) {
        wc_add_notice( 'You qualify for free shipping!', 'success' );
    } else {
        wc_add_notice( $add_more, 'notice' );
    }	

}

Now the only thing we need to do is add this function to the right action in WooCommerce. We want it to display in the cart and to reflect updates people make to the cart in real-time. Users can add/remove cart items without refreshing the page. We need this notice to update without a page refresh so it always has the correct information. To do that we need to add this to two separate actions.

add_action( 'woocommerce_check_cart_items', 'ijab_free_shipping_cart_notice', 10, 0 );
add_action( 'woocommerce_checkout_order_review', 'ijab_free_shipping_cart_notice', 10, 0 );

This will show our notices in the cart and at checkout. Here’s a screenshot of how this can look.

Here’s the full snippet you can use. Place it in your theme’s functions.php file or with a plugin like Code Snippets.

function ijab_free_shipping_cart_notice() {
    if ( is_admin() ) return;
    
    global $woocommerce;
    $total = WC()->cart->get_cart_contents_total();
	$threshold = 2500;
    $balance =  $threshold - $total;
	$formatted_balance = number_format($balance, 2, '.', ',');
	
	$add_more = 'Add $'.$formatted_balance.' more for free shipping!';

    if ( $balance < 0 ) {
        wc_add_notice( 'You qualify for free shipping!', 'success' );
    } else {
        wc_add_notice( $add_more, 'notice' );
    }	

}
add_action( 'woocommerce_check_cart_items', 'ijab_free_shipping_cart_notice', 10, 0 );
add_action( 'woocommerce_checkout_order_review', 'ijab_free_shipping_cart_notice', 10, 0 );

If you have any questions, let me know in the comments below.

Leave a Reply

%d bloggers like this: