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' );
}
}
}
@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