3

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
microb14
  • 453
  • 4
  • 16

1 Answers1

3

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thanks but i tried with a discount code. The total with discount is 40 € and it doesn't work – microb14 Jun 16 '20 at 10:26
  • @MichaelRobert Sorry there was a missing `;` … try it again, it works… – LoicTheAztec Jun 16 '20 at 15:46
  • @MichaelRobert For me it works nicely, tested on last WooCommerce version under storefront theme. – LoicTheAztec Jun 16 '20 at 20:31
  • 1
    @MichaelRobert does it make a difference when you replace `$discount_total = WC()->cart->discount_total;` with `$discount_total = WC()->cart->get_discount_total();` ? – 7uc1f3r Jun 17 '20 at 08:57
  • It's the same thing: look to [get_discount_total() method source code](https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#228-236) … WC_Cart class still allow to access properties directly. – LoicTheAztec Jun 17 '20 at 15:17
  • @MichaelRobert nice to hear, don't forget to [accept](https://stackoverflow.com/help/accepted-answer) Loic's answer. Regards – 7uc1f3r Jun 17 '20 at 16:06
  • 2
    I have updated my answer… Strange because both `$discount_total = WC()->cart->discount_total;` and `$discount_total = WC()->cart->get_discount_total();` work on different WC versions for me. – LoicTheAztec Jun 17 '20 at 16:08
  • 2
    @LoicTheAztec when debugging I noticed that the value was left at 0, I only use 1 version of WC, that is the latest (4.2.0). So I can't compare it to other versions. Nor can I immediately explain the difference. – 7uc1f3r Jun 17 '20 at 16:15
  • 2
    @7uc1f3r I have mostly all major versions since version 2.4 for testing :) – LoicTheAztec Jun 17 '20 at 16:39