简体   繁体   中英

Set a minimum order amount for specific postcodes in WooCommerce

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM