1

The following code based on an official code snippet from WooCommerce set a minimal required total amount, to be able to 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 = 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' 
            );
        }
    }
}

How can I set a certain minimum order amount in WooCommerce but only for specific postal codes?

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ECDJ
  • 79
  • 8

1 Answers1

4

To set a minimum order amount for specific postcodes in WooCommerce, try the following:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
    $minimum   = 50; // Set the minimum order value
    $postcodes = array('99152', '99154'); // Define your targeted postcodes in the array
    $postcode  = ''; // Initializing

    if ( isset($_POST['shipping_postcode']) && ! empty($_POST['shipping_postcode']) ) {
        $postcode = $_POST['shipping_postcode'];
        if ( empty($postcode) && isset($_POST['billing_postcode']) && ! empty($_POST['billing_postcode']) ) {
            $postcode = $_POST['billing_postcode'];
        }
    } elseif ( $postcode = WC()->customer->get_shipping_postcode() ) {
        if ( empty($postcode) ) {
            $postcode = WC()->customer->get_billing_postcode();
        }
    }

    if ( WC()->cart->total < $minimum && ! empty($postcode) && in_array( $postcode, $postcodes ) ) {
        $error_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 )
        );

        if( is_cart() ) {
            wc_print_notice( $error_notice, 'error' );
        } else {
            wc_add_notice( $error_notice, 'error' );
        }
    }
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Awesome! In my case I actually have two array's of postal codes with both a different minimum order amount, how could I implement this? And also postal codes in my countries are formatted like this: 1111 AA. But the rule should be applied too all postal codes with 1111, no matter what the two letters are behind it. How would I implement that? – ECDJ Apr 08 '21 at 11:16
  • I am also interested, if you have two arrays of postal codes with different order amounts. Is that possible? 10012, 10013 = 15€ min. order value and 10021, 10022 = 25€ min. order value... How is it possible to implement this? – ASSolution Apr 29 '22 at 13:42