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