-
-
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' ); | |
} | |
} | |
} |
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' );
}
}
}`
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?
I want to add a functionality for pincode wise minimum order amount
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.
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' );
}
}
}
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() {
}
Thanks


