Different Minimum Order Amounts Based on Shipping Zone

I am trying to implement a different minimum order requirement based on the shipping zone in woocommerce and I can't seem to get this to work. The $49.99 minimum still seems to be in effect despite someone's address being located in Zone 3.

// MINIMUM CART AMOUNT FOR CHECKOUT

add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
    $zone = WC_Shipping_Zones::get_zone_by ( 'zone_name', "1" . 'zone_name', "2" . 'zone_name' , "3" );

    // Only run it in  Cart or Checkout pages
    if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
        // Get cart shipping packages
        $shipping_packages =  WC()->cart->get_shipping_packages();

        // Get the WC_Shipping_Zones instance object for the first package
        $shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
        $zone_id   = $shipping_zone->get_id(); // Get the zone ID
        $zone_name = $shipping_zone->get_zone_name(); // Get the zone name

        // Total (before taxes and shipping charges)
        $total = WC()->cart->subtotal;

         // HERE Set minimum cart total amount (for Zone 1,2,3 and for other zones)
       // $min_total = $zone_name == ״1״ ? 99.99 : 200.0;
$min_total = 200;
if  ($zone_name == "2") $min_total = 300;
if ($zone_name == "3") $min_total = 350;

        // Add an error notice is cart total is less than the minimum required
   if( $total <= $min_total  ) {
            // Display an error message
 wc_add_notice(   sprintf(
                __("minimum order is - %s "),
                wc_price( $min_total)
            )  , 'error' );
        }
    }
}

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:

enter image description here


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.

Setting A Minimum Number Of Products Required Per Order

Another significant scenario is setting a minimum number of products that must be requested at once before permitting the client to completely pay for his request and dispatching the goods. Change “10” to whatever works best for your requirements. You need to ensure that you just run this code on the cart and checkout pages. For this we utilize is_cart() and is_checkout(), which return ‘true’ at whatever point we are on those two particular pages.

Put the following code in functions.php which located in theme folder.

  1. // Set a minimum number of products requirement before checking out
  2. add_action( ‘woocommerce_check_cart_items', ‘cw_min_num_products' );
  3. function cw_min_num_products() {
  4. // Only run in the Cart or Checkout pages
  5. if( is_cart() || is_checkout() ) {
  6. global $woocommerce;
  7. // Set the minimum number of products before checking out
  8. $minimum_num_products = 10;
  9. // Get the Cart's total number of products
  10. $cart_num_products = WC()->cart->cart_contents_count;
  11. // Compare values and add an error is Cart's total number of products
  12. // happens to be less than the minimum required before checking out.
  13. // Will display a message along the lines of
  14. if( $cart_num_products < $minimum_num_products ) {
  15. // Display our error message
  16. wc_add_notice( sprintf( ‘<strong>A Minimum of %s products is required before checking out.</strong>'
  17. . ‘<br />Current number of items in the cart: %s.',
  18. $minimum_num_products,
  19. $cart_num_products ),
  20. ‘error' );
  21. }
  22. }
  23. }

minimum product requirement

Setting a Minimum Quantity per Product

Setting a minimum amount for every product is a common necessity for WooCommerce stores, especially if you are offering wholesale. Setting a minimum amount will restrict your customer(s) from buying a particular product  in lesser amounts. For us to check for the minimum amounts, we have to loop through each and every product in the cart and check it against our minimum  quantity per product necessities set up.

To set these restrictions, you have to make an array which holds your rules/limitations inside another array. Be careful when altering this array, and ensure that all code is entered precisely in order to prevent errors and unexpected results. The format you need to use is as follows:

  1. // Product Id and Min. Quantities per Product
  2. // id = Product ID
  3. // min = Minimum quantity
  4. $product_min_qty = array(
  5. array( ‘id' => 27, ‘min' => 8 ),
  6. array( ‘id' => 25, ‘min' => 8 ),
  7. array( ‘id' => 23, ‘min' => 8 ),
  8. array( ‘id' => 20, ‘min' => 8 ),
  9. );
  10. Here is where the magic happens.
  11. // Set a minimum number of products requirement before checking out
  12. // Set minimum quantity per product before checking out
  13. add_action( ‘woocommerce_check_cart_items', ‘cw_set_min_qty_per_product' );
  14. function cw_set_min_qty_per_product() {
  15. // Only run in the Cart or Checkout pages
  16. if( is_cart() || is_checkout() ) {
  17. global $woocommerce;
  18. // Product Id and Min. Quantities per Product
  19. $product_min_qty = array(
  20. array( ‘id' => 27, ‘min' => 8 ),
  21. array( ‘id' => 25, ‘min' => 8 ),
  22. array( ‘id' => 23, ‘min' => 8 ),
  23. array( ‘id' => 20, ‘min' => 8 ),
  24. );
  25. // Will increment
  26. $i = 0;
  27. // Will hold information about products that have not
  28. // met the minimum order quantity
  29. $bad_products = array();
  30. // Loop through the products in the Cart
  31. foreach( $woocommerce->cart->cart_contents as $product_in_cart ) {
  32. // Loop through our minimum order quantities per product
  33. foreach( $product_min_qty as $product_to_test ) {
  34. // If we can match the product ID to the ID set on the minimum required array
  35. if( $product_to_test[‘id'] == $product_in_cart[‘product_id'] ) {
  36. // If the quantity required is less than than the quantity in the cart now
  37. if( $product_in_cart[‘quantity'] < $product_to_test[‘min'] ) {
  38. // Get the product ID
  39. $bad_products[$i][‘id'] = $product_in_cart[‘product_id'];
  40. // Get the Product quantity already in the cart for this product
  41. $bad_products[$i][‘in_cart'] = $product_in_cart[‘quantity'];
  42. // Get the minimum required for this product
  43. $bad_products[$i][‘min_req'] = $product_to_test[‘min'];
  44. }
  45. }
  46. }
  47. // Increment $i
  48. $i++;
  49. }
  50. // Time to build our error message to inform the customer
  51. // About the minimum quantity per order.
  52. if( is_array( $bad_products) && count( $bad_products ) > 1 ) {
  53. // Lets begin building our message
  54. $message = ‘<strong>A minimum quantity per product has not been met.</strong><br />';
  55. foreach( $bad_products as $bad_product ) {
  56. // Append to the current message
  57. $message .= get_the_title( $bad_product[‘id'] ) .‘ requires a minimum quantity of ‘
  58. . $bad_product[‘min_req']
  59. .‘. You currently have: ‘. $bad_product[‘in_cart'] .‘.<br />';
  60. }
  61. wc_add_notice( $message, ‘error' );
  62. }
  63. }
  64. }

Here is how it will look on your website:

minimum quantity requirement woocommerce

Conclusion

WooCommerce permits you to utilize actions and filters to change the ordinary checkout process. All stores are distinctive and having the capacity to set these restrictions — when required — is crucial. For us developers, who need to accomplish tasks like these, knowing how to alter shipping rules is key.

 
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.