Muhahaha
Muhahaha

Reputation: 67

How to set minimum order amount for WooCommerce excluding shipping?

Is there a code to set minimum order amount for WooCommerce excluding shipping?

With this one I can set minimum order amount but with shipping:

/**
 * Set a minimum order amount for checkout
 */
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 = 16;

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

        if( is_cart() ) {

            wc_print_notice( 
                sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        } else {

            wc_add_notice( 
                sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        }
    }
}

Upvotes: 2

Views: 1728

Answers (1)

H. Bloch
H. Bloch

Reputation: 488

As I mentioned in the comments,

You just need to subtract the cost of shipping from the cart total. You can use the WC_Cart::get_shipping_total method.

It should look something like this:

<?php
/**
 * Set a minimum order amount for checkout
 */
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 = 16;
    $cart_total = WC()->cart->total; // Cart total incl. shipping
    $shipping_total = WC()->cart->get_shipping_total();  // Cost of shipping
    if ( ($cart_total - $shipping_total) < $minimum ) {

        if( is_cart() ) {

            wc_print_notice( 
                sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        } else {

            wc_add_notice( 
                sprintf( 'Стойност на Вашата поръчка: %s <p>Минималната стойност на поръчката към ресторанта: %s' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        }
    }
}
?>

Upvotes: 5

Related Questions