Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Set Minimum Weight for a Specific Country in WooCommerce Cart?

Do you want to set the minimum weight for the products based on a specific country on the WooCommerce cart page? Then this code snippet checks the cart’s total weight on the cart pages and displays an error notice if the total weight is below a specified minimum for customers in India (‘IN’).

add_action( 'woocommerce_check_cart_items', 'ts_required_min_weight_country_based' );
function ts_required_min_weight_country_based() {
    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'];
    }

    if( in_array( $country, array('IN') ) ){

        // HERE Set the minimum weight
        $minimum_weight = 7; // 7 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' );
        }
    }
}

Output

Let’s see how the weight of a product is set from the admin side with the following steps mentioned below.

  • From the WordPress admin sidebar, click on “Products”.
  • Locate the product you want to set the weight & click on its name and “Edit” link.
  • Under “Product Data” select “Shipping”, find fields for Weight, enter the values, and click on Update.
How to Set Minimum Weight for a Specific Country in WooCommerce Cart

When the cart item weight is less than what is specified in the minimum weight, then the error notice message will be displayed based on the country selection during the checkout process.

How to Set Minimum Weight for a Specific Country in WooCommerce Cart

Additionally, you can also hide shipping methods based on product weight. You can refer to our guide that will hide WooCommerce shipping methods for certain conditions such as weight and product quantity, etc.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
2 Comments
Newest
Oldest
Inline Feedbacks
View all comments
riddhi
7 months ago

do you have a youtube tutorial for the same?

Editor
7 months ago
Reply to  riddhi

Hi Riddhi,

Thank you for your inquiry! We don’t have a YouTube tutorial specifically covering this topic, but we do have some similar kind of topics and you can check out on our YouTube channel. Feel free to ask if you need further clarification or guidance on implementing it. 

2
0
Would love your thoughts, please comment.x
()
x