Set a minimum order amount in WooCommerce
Asked Answered
P

3

6

I want to have a minimum order amount in my WooCommerce store. The following code is perfectly showing a notice if the amount isn't reached but the checkout is still possible. How to disable checkout-button when the minimum amount isn't reached?

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

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

    if ( WC()->cart->total < $minimum ) {

        if( is_cart() ) {

            wc_print_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        } else {

            wc_add_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        }
    }
}
Passive answered 7/3, 2019 at 7:41 Comment(1)
Try using the woocommerce_check_cart_items hook instead.Karrykarst
M
15

To set a minimum order amount you can use woocommerce_check_cart_items action hook this way:

add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {

    // HERE Set minimum cart total amount
    $minimum_amount = 250;

    // Total (before taxes and shipping charges)
    $cart_subtotal = WC()->cart->subtotal;

    // Add an error notice is cart total is less than the minimum required
    if( $cart_subtotal < $minimum_amount  ) {
        // Display an error message
        wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '</strong>', 'error' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

If customer update the cart changing quantities or removing items, The behavior will be updated too.


enter image description here


enter image description here


Related answer: Woocommerce set minimum order for a specific user role

Mesocratic answered 7/3, 2019 at 9:56 Comment(1)
That works just fine Thank you! though, a closing <strong> tag is missing or it may break the style.Pearly
U
4
function disable_checkout_button() { 

    // Set this variable to specify a minimum order value
    $minimum = 50;
    $total = WC()->cart->cart_contents_total;
    if( $total < $minimum ){
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        echo '<a style="pointer-events: none !important;" href="#" class="checkout-button button alt wc-forward">Proceed to checkout</a>';
    }  
}

add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button', 1 );
Uncontrollable answered 7/3, 2019 at 8:24 Comment(3)
thanks, this is in general working, but if you then increase cart amount, button is still not working.Passive
I found error in this code. Replace : $total = WC()->cart->get_cart_subtotal(); on $total = WC()->cart->cart_contents_total; - get Cart Total as number.Arsenide
@RuslanNovikov If it's tested ok, you may please modify the answerUncontrollable
N
0
// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set minimum cart total
        $minimum_cart_total = 500;

        // Total we are going to be using for the Math
        // This is before taxes and shipping charges
        $total = WC()->cart->subtotal;
        
        // Compare values and add an error is Cart's total
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of USD 500 is required before checking out. (Cont. below)
        // Current cart total: USD 6 
        if( $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A minimum order value of %s is required before checking out.</strong>'
                .'<br />Your current value is: %s',
                wc_price( $minimum_cart_total ),
                wc_price( $total ) ),
            'error' );
        }
    }
}
Nourish answered 11/9, 2021 at 7:13 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Nook
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Nook

© 2022 - 2025 — McMap. All rights reserved.