skip to Main Content

In WooCommerce, I am using the following code to set a mandatory minimum order amount for checkout:

/**
 * 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 = 15;
    $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( 'Your current order total is %s — you must have an order with a minimum of %s excl shipping 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 excl shipping to place your order' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        }
    }
}

But I would like to use instead the cart subtotal excluding VAT and shipping. Actually I am getting the cart total including VAT and shipping.

How can I get the cart subtotal excluding VAT and shipping?

2

Answers


  1. You need to get the cart subtotal excluding vat, using WC_Cart get_subtotal() method instead of using the "total" property.

    Also, your code can be simplified a bit.

    Try the following replacement:

    /**
     * Minimum order subtotal amount for checkout (excl shipping and taxes)
     */
    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
     
    function wc_minimum_order_amount() {
        $minimum  = 15; // <=== Here specify the minimum order amount
        $subtotal = WC()->cart->get_subtotal(); // Cart subtotal excl. shipping and taxes
    
        if ( $subtotal < $minimum ) {
            $message = sprintf( 
                esc_html__('Your current order subtotal is %s. To place your order, you must have a minimum amount of %s (excl. shipping and VAT).', 'woocommerce'), 
                wc_price( $subtotal ), 
                wc_price( $minimum )
            );
    
            if( is_cart() ) {
                wc_print_notice( $message, 'error' );
            } else {
                wc_add_notice( $message, 'error' );
            }
        }
    }
    

    It should work as expected.

    Login or Signup to reply.
  2. Try this :

    /**
     * Set a minimum order amount for checkout in WooCommerce
     */
    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart', 'wc_minimum_order_amount' );
    
    function wc_minimum_order_amount() {
        // Set the minimum order amount (excluding shipping)
        $minimum = 15;
        
        // Get the cart total excluding shipping
        $cart_total_excl_shipping = WC()->cart->subtotal;
    
        if ( $cart_total_excl_shipping < $minimum ) {
            $message = sprintf(
                __( 'Your current order total is %s. You must have an order with a minimum of %s excluding shipping to place your order.', 'woocommerce' ),
                wc_price( WC()->cart->total ),
                wc_price( $minimum )
            );
    
            if ( is_cart() ) {
                wc_print_notice( $message, 'error' );
            } else {
                // Display error message at checkout
                wc_add_notice( $message, 'error' );
            }
        }
    }
    

    This solution should now work as expected

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search