WooCommerce

How to Set a Minimum Order Amount for Checkout

How to Set a Minimum Order Amount for Checkout

If you sell cheaper products in your store, and sometimes it happens when the shipping amount is more than the products cost in the order. Of course, you can send such orders, but often you have to set a minimum order amount to avoid such cases.

Fortunately for woocommerce it can be done very simply and you don’t need to install any additional plugins. Just add this code snippet to your child theme’s functions.php

First example: Set a Minimum Order Amount with Shipping Cost

//Set a minimum order amount for checkout
add_action( 'woocommerce_before_cart' , 'warp_minimum_order_amount' );
add_action( 'woocommerce_before_checkout_form' , 'warp_minimum_order_amount' );

function warp_minimum_order_amount()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->total < $minimum )
    {
        if( is_cart() || is_checkout() )
        {
            wc_print_notice(
                sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
                    wc_price( WC()->cart->total ),
                    wc_price( $minimum )
                ), 'error'
            );
        }
    }
}

add_action( 'woocommerce_checkout_process', function()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->total < $minimum )
    {
        throw new Exception( sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
            wc_price( WC()->cart->total ),
            wc_price( $minimum )
        ) );
    }
} );

Second example: Set a Minimum Order Amount without Shipping Cost

//Set a minimum order amount for checkout
add_action( 'woocommerce_before_cart' , 'warp_minimum_order_amount' );
add_action( 'woocommerce_before_checkout_form' , 'warp_minimum_order_amount' );

function warp_minimum_order_amount()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->subtotal < $minimum )
    {
        if( is_cart() || is_checkout() )
        {
            wc_print_notice(
                sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
                    wc_price( WC()->cart->subtotal ),
                    wc_price( $minimum )
                ), 'error'
            );
        }
    }
}

add_action( 'woocommerce_checkout_process', function()
{
    // Set this variable to specify a minimum order value
    $minimum = 1000;

    if ( WC()->cart->subtotal < $minimum )
    {
        throw new Exception( sprintf( esc_html__( 'Your current order total is %s — you must have an order with a minimum of %s to place your order', 'your text domain' ) ,
            wc_price( WC()->cart->subtotal ),
            wc_price( $minimum )
        ) );
    }
} );

Leave a comment

Subscribe to our Newsletter

Something BIG is coming, you'll be the first to know about it.