Reputation: 3
I have two array's of postal codes with both a different minimum order amount, how could I implement this? If you have two arrays of postal codes with different order amounts. Is that possible? For Pincode 10001 & 10002 = 200 min order value and for Pincode 10003, 10004 = 300 min. order value... How is it possible to implement this. Is there any code snippet available for this?
Upvotes: 0
Views: 278
Reputation: 11282
You can use woocommerce_checkout_process
action hooks which allow you to validate checkout before placing an order.
Create an array with postal code and its value like below.
$minimum = array(
'10001' => 200,
'10002' => 200,
'10003' => 300,
'10004' => 300
);
You can get cart subtotal with WC()->cart->subtotal
and compare it with the minimum postal code amount.
Complete code. Code will go in your active theme functions.php
add_action( 'woocommerce_checkout_process', 'set_a_minimum_order_amount_per_zip_code', 9999999 );
function set_a_minimum_order_amount_per_zip_code() {
global $woocommerce;
//add postal code to array with value the minimum amount without spances//
$minimum = array(
'10001' => 200,
'10002' => 200,
'10003' => 300,
'10004' => 300
);
$billing_postcode = isset( $_POST["billing_postcode"] ) ? str_replace( " ", "", $_POST["billing_postcode"] ) : "";
if ( array_key_exists( $billing_postcode, $minimum ) && ( WC()->cart->subtotal < $minimum[$billing_postcode] ) ){
wc_add_notice(
sprintf( __( 'You must have an order with a minimum of %s to place your order, your current order total is %s.', 'woofood-plugin' ) ,
wc_price( $minimum[$billing_postcode] ),
wc_price( WC()->cart->subtotal )
), 'error'
);
}
}
Tested and works
Upvotes: 1