free-shipping threshold messages
|

Add “spend xx more and get free shipping” message on top of cart page

Here’s the code snippet to add a “spend xx more and get free shipping” message on top of cart page or the checkout page:

<?php

// Define a function that will check and display a notice about free shipping threshold
function ace__free_shipping_threshold_notice() {
    // Check if we're on the cart page - is_cart() is a WooCommerce function that returns true only on cart page
    if ( is_cart() ) {
        // WC() is the main WooCommerce class instance
        // ->cart accesses the cart object
        // ->get_shipping_packages() gets all items in cart that need shipping
        $packages = WC()->cart->get_shipping_packages();
        
        // reset() gets the first element of an array
        // We only need the first package as shipping is usually calculated per cart
        $package  = reset( $packages );
        
        // Get the shipping zone based on customer's address
        // Shipping zones determine available shipping methods for the customer's location
        $zone     = wc_get_shipping_zone( $package );

        // Get the cart subtotal (before shipping)
        $cart_total = WC()->cart->get_displayed_subtotal();
        
        // Calculate any discounts that need to be deducted
        // If prices include tax, we also deduct the discount tax
        $deduct     = ( WC()->cart->get_discount_total() + 
                       ( WC()->cart->display_prices_including_tax() ? 
                         WC()->cart->get_discount_tax() : 0 ) );
        
        // Calculate the final cart total after deducting discounts
        // wc_get_price_decimals() gets the number of decimal places set in WooCommerce
        $cart_total = round( $cart_total - $deduct, wc_get_price_decimals() );

        // Loop through all enabled shipping methods in this zone
        foreach ( $zone->get_shipping_methods( true ) as $method ) {
            // Skip if this is not a free shipping method
            if ( $method->id !== 'free_shipping' ) continue;

            // Get the minimum amount required for free shipping
            $min_amount = $method->get_option( 'min_amount' );
            
            // If there is a minimum amount set AND cart total is less than that
            if ( ! empty( $min_amount ) && $cart_total < $min_amount ) {
                // Calculate how much more customer needs to spend
                $remaining = $min_amount - $cart_total;
                
                // Add a notice to the cart page
                // wc_price() formats the price according to store settings
                // 'notice' is the type of message (others could be 'error' or 'success')
                wc_add_notice( sprintf( 'Spend %s more to get free shipping!', 
                             wc_price( $remaining ) ), 'notice' );
            }
        }
    }
}

// Hook our function to run before the cart is displayed
// 'woocommerce_before_cart' is a WooCommerce action hook that fires at the start of cart page
add_action( 'woocommerce_before_cart', 'ace__free_shipping_threshold_notice' );

It will show like this

free-shipping threshold messages

Leave a Reply

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