Search code examples
phpwordpresswoocommercecartdiscount

Set a minimum undiscounted order amount in WooCommerce


I am using "Minimum Order Amount" to require a minimum order amount.

If I have a basket of € 50 of purchase and I apply my 10% discount code, I can't order my shopping cart because the total is € 45.

But I want to order for < € 50 ONLY with a discount code


Solution

  • Updated: Use the following revisited and compacted code, that uses the undiscounted total:

    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;
    
        $total = WC()->cart->total;
        $discount_total = WC()->cart->get_discount_total(); // updated thanks to 7uc1f3r
        $maximized_total = $total + $discount_total;
    
        if ( $maximized_total < $minimum ) {
    
            $notice = sprintf( __('Your current order total is %s — you must have an order with a minimum of %s to place your order '), 
                wc_price( $maximized_total ), 
                wc_price( $minimum )
            );
    
            if( is_cart() ) {
                wc_print_notice( $notice , 'error' );
            } else {
                wc_add_notice( $notice , 'error' );
            }
        }
    }
    

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