I have revisited your code and make it work just for colombia with a minimal weight of 20 kilos. You will need to check the weight unit as it should be “Kg” (in Kilos).
The code:
add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight_country_based' );
function checkout_required_min_weight_country_based() {
// Only on Cart or Checkout pages
if( ! ( is_cart() || is_checkout() ) ) return;
// Get the shipping country
$country = WC()->session->get('customer')['shipping_country'];
if( empty($country) ){
$country = WC()->session->get('customer')['billing_country'];
}
// For Colombia and Argentina shipping countries
if( in_array( $country, array('CO', 'AR') ) ){
// HERE Set the minimum weight
$minimum_weight = 20; // 20 kg
// Get the Cart's content total weight
$total_weight = WC()->cart->get_cart_contents_weight();
// If total weight is lower than minimum, we avoid checkout and display an error notice
if( $total_weight < $minimum_weight ) {
// Display an dynamic error notice
wc_add_notice( sprintf(
'<strong>A Minimum Weight of %s is required before checking out.</strong>'
. '<br />Current cart weight: %s',
wc_format_weight($minimum_weight),
wc_format_weight($total_weight)
), 'error' );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
When Colombia is the detected country (or the defined country) you will get something like:
The same code for all countries:
add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight' );
function checkout_required_min_weight() {
// Only on Cart or Checkout pages
if( ! ( is_cart() || is_checkout() ) ) return;
// HERE Set the minimum weight
$minimum_weight = 20; // 20 kg
// Get the Cart's content total weight
$total_weight = WC()->cart->get_cart_contents_weight();
// If total weight is lower than minimum, we avoid checkout and display an error notice
if( $total_weight < $minimum_weight ) {
// Display an dynamic error notice
wc_add_notice( sprintf(
'<strong>A Minimum Weight of %s is required before checking out.</strong>'
. '<br />Current cart weight: %s',
wc_format_weight($minimum_weight),
wc_format_weight($total_weight)
), 'error' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
You are currently using:
$state = array('BG-23');
$cart_total_order = WC()->cart->total;
if( $cart_total_order < $minimum && in_array(WC()->customer->get_shipping_state(), $state ) ) {
You’ll want to use something like this:
$state = $woocommerce->customer->get_shipping_state();
if( $cart_total_order < $minimum && $state == 'BG-23' ) {
Let me know if that helps.
The following code works for me, so maybe modify this to fit your specific needs.
In the case, I am requiring a minimum order value of $25 if the state is CA (California). It does not allow checkout. Instead, it displays an error that there is a problem in the cart, and the cart itself echoes the error message within the code:
add_action( 'woocommerce_check_cart_items', 'wc_min_order_total' );
function wc_min_order_total() {
if( is_cart() || is_checkout() ) {
global $woocommerce;
$minimum = 25;
$state = array('CA'); //California
$cart_tot_order = WC()->cart->total;
$state = $woocommerce->customer->get_shipping_state();
if( $cart_total_order < $minimum && $state == 'CA' ) {
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' );
}
}
}
You can extend the array and add more states if you like.