Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pattikawa/146f8a307d2c03bc7f35e9601fc5de31 to your computer and use it in GitHub Desktop.
Save pattikawa/146f8a307d2c03bc7f35e9601fc5de31 to your computer and use it in GitHub Desktop.
// Set a minimum amount of oder based on shipping zone before checking out
add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );
// Only run in the Cart or Checkout pages
function cw_min_num_products() {
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum order amount and shipping zone before checking out
$minimum = 20;
$county = array('NL');
// Defining var total amount
$cart_tot_order = WC()->cart->total;
// Compare values and add an error in Cart's total amount
// happens to be less than the minimum required before checking out.
// Will display a message along the lines
if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) ) {
// Display error message
wc_add_notice( sprintf( '<strong>A Minimum order of $%s is required before checking out.</strong>'
. '<br />Current order: $%s.',
$minimum,
$cart_tot_order ),
'error' );
}
}
}
@pattikawa
Copy link
Author

copy paste the code using plugin code snippets
https://wordpress.org/plugins/code-snippets/

@asaphtunes
Copy link

Nice code.

I have 2 questions:

  1. do i paste this in the functions.php file?

  2. how do iadd which countries i want to use this code for? For example, I only want this to be applied to USA and Canada orders

@shishir3005
Copy link

Hi,
Could you please let me know how can I replace $county = array('NL'); for specific UK post code.
$postcode = array("IG10*");
is it good?

Thanks

@Ansolon
Copy link

Ansolon commented Jun 9, 2019

very good code thanks; i also want to do with special post code

how can we replace $county = array('NL');

with postal code?

@brandsign
Copy link

Hi,

Here the snippet for postal code (customer shipping address) instead county:

// Set a minimum amount of oder based on shipping postcode before checking out

add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {
	
	if( is_cart() || is_checkout() ) {
		global $woocommerce;

		// Set the minimum order amount and shipping postcode before checking out
        $minimum = 20;
	  	$shipping_postcode 	= array('WC2N 5DU');
		
		// Defining var total amount      
        $cart_tot_order = WC()->cart->total;
        
	  	// Compare values and add an error in Cart's total amount
	    // happens to be less than the minimum required before checking out.
		// Will display a message along the lines 
     
		if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_postcode(), $shipping_postcode )  ) {
			// Display error message
	        wc_add_notice( sprintf( '<strong>A Minimum order of $%s is required before checking out.</strong>' 
	        	. '<br />Current order: $%s.',
	        	$minimum,
                $cart_tot_order	),
	        'error' );
		}
	}
}

Does anyone know how to manage multiple entries respectively in this case multiple postal codes with related minimum order amount?

Thanks!

@sim2
Copy link

sim2 commented May 18, 2020

Hi!

Thank you SO much for this code, I've been searching everywhere for such a plugin.
I was wondering, if I wanted to create a second minimum for a new set of postal codes, how do I create that if statement?

Thanks!

@soringurz
Copy link

Hello!

I want to replace "$county = array('NL');" with cities, I've tried $city but it didn't work.

Could you please help me out? Thanks.

@pattikawa
Copy link
Author

pattikawa commented May 24, 2020 via email

@sim2
Copy link

sim2 commented May 24, 2020

Hello!

I want to replace "$county = array('NL');" with cities, I've tried $city but it didn't work.

Could you please help me out? Thanks.

Hi, I believe the correct way to input it would be $shipping_city

@domenig
Copy link

domenig commented Jul 2, 2020

Hi all!
Here's what I tried to use to not only filter by country but for the shipping method. I do want to achieve that the minimum order quantity is not valid when the customer selects local pickup as a shipping method.
It would be great if somebody knows how to do this.

Thanks already in advance!

`// Set a minimum amount of order based on shipping zone & shipping method before checking out

add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {

if( is_cart() || is_checkout() ) {
	global $woocommerce;

	// Set the minimum order amount, shipping zone & shipping method before checking out
    $minimum = 75;
  	$county 	= array('CH');
	$shipping_method 	= 'local_pickup';
	
	// Defining var total amount      
    $cart_tot_order = WC()->cart->total;
    
  	// Compare values and add an error in Cart's total amount
    // happens to be less than the minimum required before checking out.
	// Will display a message along the lines 
 
	if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && WC()->shipping->get_shipping_methods() != $shipping_method ) {
		// Display error message
        wc_add_notice( sprintf( '<strong>Sie haben die Mindestbestellmenge von $%s EUR noch nicht erreicht. Geringerer Bestellwert ist nur bei Selbstabholung möglich.</strong>' 
        	. '<br />Ihre derzeitige Bestellmenge ist: $%s.',
        	$minimum,
            $cart_tot_order	),
        'error' );
	}
}

}`

@domenig
Copy link

domenig commented Jul 2, 2020

Here I made minor changes, as I would also like to include a second country. Still does not work, unfortunately.

// Set a minimum amount of order based on shipping zone & shipping method before checking out

add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {
	
	if( is_cart() || is_checkout() ) {
		global $woocommerce;

		// Set the minimum order amount, shipping zone & shipping method before checking out
        $minimum = 75;
	  	$county 	= array('CH') || array('LI');
		$shipping_method 	= 'local_pickup';
		
		// Defining var total amount      
        $cart_tot_order = WC()->cart->total;
        
	  	// Compare values and add an error in Cart's total amount
	    // happens to be less than the minimum required before checking out.
		// Will display a message along the lines 
     
		if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && WC()->shipping->get_shipping_methods() != $shipping_method ) {
			// Display error message
	        wc_add_notice( sprintf( '<strong>Sie haben die Mindestbestellmenge von $%s EUR noch nicht erreicht. Geringerer Bestellwert ist nur bei Selbstabholung möglich.</strong>' 
	        	. '<br />Ihre derzeitige Bestellmenge ist: $%s.',
	        	$minimum,
                $cart_tot_order	),
	        'error' );
		}
	}
}

@sim2
Copy link

sim2 commented Jul 2, 2020

Hi all!
Here's what I tried to use to not only filter by country but for the shipping method. I do want to achieve that the minimum order quantity is not valid when the customer selects local pickup as a shipping method.
It would be great if somebody knows how to do this.

Thanks already in advance!

`// Set a minimum amount of order based on shipping zone & shipping method before checking out

add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {

if( is_cart() || is_checkout() ) {
	global $woocommerce;

	// Set the minimum order amount, shipping zone & shipping method before checking out
    $minimum = 75;
  	$county 	= array('CH');
	$shipping_method 	= 'local_pickup';
	
	// Defining var total amount      
    $cart_tot_order = WC()->cart->total;
    
  	// Compare values and add an error in Cart's total amount
    // happens to be less than the minimum required before checking out.
	// Will display a message along the lines 
 
	if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && WC()->shipping->get_shipping_methods() != $shipping_method ) {
		// Display error message
        wc_add_notice( sprintf( '<strong>Sie haben die Mindestbestellmenge von $%s EUR noch nicht erreicht. Geringerer Bestellwert ist nur bei Selbstabholung möglich.</strong>' 
        	. '<br />Ihre derzeitige Bestellmenge ist: $%s.',
        	$minimum,
            $cart_tot_order	),
        'error' );
	}
}

}`

Hey,

I'm not exactly sure if I understand correctly what you are trying to accomplish. If you are just trying to make it that the minimum is turned off when a user is choosing local pickup, you can make the if statement into just "if chosen shipping is flat-rate" in which case all other methods (I.E. local_pickup) won't get affected by the if statement.

@domenig
Copy link

domenig commented Jul 2, 2020

Thank you so much for your input. I was able to achieve my desired result with this code.

// Set a minimum amount of order based on shipping zone & shipping method before checking out

add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {
	
	if( is_cart() || is_checkout() ) {
		global $woocommerce;

		// Set the minimum order amount, shipping zone & shipping method before checking out
        $minimum = 75;
	  	$county 	= array('CH','LI');
		$chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
		$chosen_shipping = explode(':', $chosen_shipping);
		
		// Defining var total amount      
        $cart_tot_order = WC()->cart->subtotal;
        
	  	// Compare values and add an error in Cart's total amount
	    // happens to be less than the minimum required before checking out.
		// Will display a message along the lines 
     
		if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && $chosen_shipping[0] != 'local_pickup') {
			// Display error message
	        wc_add_notice( sprintf( '<strong>Sie haben die Mindestbestellmenge von €%s exklusive Versandkosten noch nicht erreicht. Geringerer Bestellwert ist nur bei Selbstabholung möglich.</strong>' 
	        	. '<br />Ihre derzeitige Bestellmenge ist: €%s.',
	        	$minimum,
                $cart_tot_order	),
	        'error' );
		}
	}
}

@mof92
Copy link

mof92 commented Aug 20, 2020

Hi!

I want to use this function, but for me is a little different.
I have a custom field with delivery zone and I want this condition based on this custom field.

I have this custom field - zona_livrare and these variation - Burdujeni, Bosanic, etc.

How can I change in this function to can work in my conditions?

@juniorthiesen
Copy link

juniorthiesen commented Oct 14, 2020

/**

  • Hide Flat Rate shipping when the minimum order.

  • @param array $rates Array of rates found for the package.

  • @return array
    */
    `
    function ts_hide_flat_shipping_for_minimum_order( $rates ) {

    $order_total = WC()->cart->get_subtotal();

if( $order_total < 150 ) {
foreach( $rates as $rate_id => $rate_val ) {
if ( 'flat_rate' === $rate_val->get_method_id() ) {
unset( $rates[ $rate_id ] );
}
}
}
return $rates;
}

add_filter( 'woocommerce_package_rates', 'ts_hide_flat_shipping_for_minimum_order', 100, 2 );
`

@ArneSaknussemm
Copy link

@juniorthiesen Thanks, that was so easy!

@ataidigital84
Copy link

ataidigital84 commented Oct 23, 2020

Hi everyone,

I'm trying to set up the snippet given by @domenig with my desired settings but it's not working (see attached error messages). I only need a minimum order amount per shipping method (delivery and collection - attached screenshot of my shipping methods implemented in one of my shipping zones). How can I adapt the following snippet with my minimum order amount per shipping method (£5 for collection and £15 for delivery)?


// Set a minimum amount of order based on shipping zone & shipping method before checking out

add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );

// Only run in the Cart or Checkout pages
function cw_min_num_products() {

if( is_cart() || is_checkout() ) {
	global $woocommerce;

	// Set the minimum order amount, shipping zone & shipping method before checking out
    $minimum = 15;
  	$shipping_postcode 	= array('DA5’, ‘DA6’, ‘DA7’, ‘DA8’, ‘DA17’, ‘DA18’);
	$shipping_method 	= array('delivery');

	
	// Defining var total amount      
    $cart_tot_order = WC()->cart->subtotal;
    
  	// Compare values and add an error in Cart's total amount
    // happens to be less than the minimum required before checking out.
	// Will display a message along the lines 
 
	if( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && $chosen_shipping[0] != 'local_pickup') {
		// Display error message
        wc_add_notice( sprintf( '<strong>Sie haben die Mindestbestellmenge von €%s exklusive Versandkosten noch nicht erreicht. Geringerer Bestellwert ist nur bei Selbstabholung möglich.</strong>' 
        	. '<br />Ihre derzeitige Bestellmenge ist: €%s.',
        	$minimum,
            $cart_tot_order	),
        'error' );
	}
}

}

Thanks
Error message 1
Error message 2
Shipping zone and methods

@dannyplace
Copy link

dannyplace commented Nov 9, 2020

Hi! I used this code to accomplish minimum order price based on selected shipping method. Just copy/paste this and it will work:

`add_action( 'woocommerce_checkout_process', 'wc_minimum_required_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_required_order_amount' );
function wc_minimum_required_order_amount() {

// HERE Your settings
$minimum_amount     = 20; // The minimum cart total amount
$shipping_method_id = 'local_pickup'; // The targeted shipping method Id (exception)

// Get some variables
$cart_total     = (float) WC()->cart->total; // Total cart amount
$chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)

// Only when a shipping method has been chosen
if ( ! empty($chosen_methods) ) {
    $chosen_method  = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
    $chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
}

// If "Local pickup" shipping method is chosen, exit (no minimun is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
    return; // exit
}

// Add an error notice is cart total is less than the minimum required
if ( $cart_total < $minimum_amount ) {
    $text_notice = sprintf(
        __("U dient tenminste voor %s te bestellen wanneer u kiest voor <b>thuisbezorgen!</b> Wanneer u kiest <b>voor afhalen geldt het minimumbedrag niet</b>. U heeft momenteel een bestelling van %s.<br><br>", "woocommerce"), // Text message
        wc_price( $minimum_amount ),
        wc_price( $cart_total )
    );
    
    if ( is_cart() ) {
        wc_print_notice( $text_notice, 'error' );
    } else {
        wc_add_notice( $text_notice, 'error' );
    }
}

}`

@blakerr
Copy link

blakerr commented Nov 17, 2020

Hi! I used this code to accomplish minimum order price based on selected shipping method. Just copy/paste this and it will work:

`add_action( 'woocommerce_checkout_process', 'wc_minimum_required_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_required_order_amount' );
function wc_minimum_required_order_amount() {

// HERE Your settings
$minimum_amount     = 20; // The minimum cart total amount
$shipping_method_id = 'local_pickup'; // The targeted shipping method Id (exception)

// Get some variables
$cart_total     = (float) WC()->cart->total; // Total cart amount
$chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)

// Only when a shipping method has been chosen
if ( ! empty($chosen_methods) ) {
    $chosen_method  = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
    $chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
}

// If "Local pickup" shipping method is chosen, exit (no minimun is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
    return; // exit
}

// Add an error notice is cart total is less than the minimum required
if ( $cart_total < $minimum_amount ) {
    $text_notice = sprintf(
        __("U dient tenminste voor %s te bestellen wanneer u kiest voor <b>thuisbezorgen!</b> Wanneer u kiest <b>voor afhalen geldt het minimumbedrag niet</b>. U heeft momenteel een bestelling van %s.<br><br>", "woocommerce"), // Text message
        wc_price( $minimum_amount ),
        wc_price( $cart_total )
    );
    
    if ( is_cart() ) {
        wc_print_notice( $text_notice, 'error' );
    } else {
        wc_add_notice( $text_notice, 'error' );
    }
}

}`

Hello, is there any way to modify that to work with shipping zone id or EXCEPT postcode?

@AbrarKhan789
Copy link

I want to add a functionality for pincode wise minimum order amount

@mihai-cristian3
Copy link

Hi there, I would like to know if there is anyway I can disable the check-out button in the cart until the minimum order amount is surpassed.

I'm also using Stirpe and Google Pay and right now I'm facing a problem with that button too: I would like to disable it if the minimum order amount is not surpassed because right now, even I'm displaying the message with the minimum amount, I can still check-out using Google Pay.

@rashedul-alam
Copy link

I want to add a functionality for pincode wise minimum order amount

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {
$minimum = ''; // Initializing
$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();
}
}

$postList =[
['9000', 25],
['9008', 20],
];// Define your targeted postcodes and fees in the array

foreach ($postList as $innerArray) {

if ($innerArray[0] ===  $postcode) {
    $minimum = $innerArray[1];
    break; 
}
else{
    $minimum = 0;
}

}

if ( WC()->cart->total < $minimum && ! empty($postcode) && $minimum !== 0 ) {
$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' );
}

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment