Last active
January 26, 2025 18:30
-
-
Save pattikawa/146f8a307d2c03bc7f35e9601fc5de31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ( 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 )
);
}
}