2

I am trying to specifically apply a mandatory minimum weight of 20 kilos for the country of Colombia avoiding checkout if the total cart weight is under this minimal weight.

Here is my actual code, that allow me to fix a minimum weight:

add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
function cldws_set_weight_requirements() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;
        // Set the minimum weight before checking out
        $minimum_weight = 30;
        // Get the Cart's content total weight
        $cart_contents_weight = WC()->cart->cart_contents_weight;
        // Compare values and add an error is Cart's total weight
        if( $cart_contents_weight < $minimum_weight  ) {
            // Display our error message
            wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>'
                . '<br />Current cart weight: %s%s',
                $minimum_weight,
                get_option( 'woocommerce_weight_unit' ),
                $cart_contents_weight,
                get_option( 'woocommerce_weight_unit' ),
                get_permalink( wc_get_page_id( 'shop' ) )
                ),
            'error' );
        }
    }
}

How can I make it work for only for Colombia country?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Manuel
  • 43
  • 1
  • 4

1 Answers1

4

Update (for Argentina and Colombia shipping counties)

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • it works perfectly !! Is it possible to implement also in the case that the customer chooses the shipping address Colombia? Thank you – Manuel Aug 29 '18 at 12:41
  • hello !, to see if I explain, the code you have sent me is perfect, but I would also like to expand and apply that any client that does not access from Colombia and applies Colombia as the country of the order, do not let you place the order if the order does not reach a minimum of 20 kilos. Thank you very much! Sorry for my translation. Greetings from Spain – Manuel Aug 29 '18 at 21:55
  • Buenas Loic, a ver sí me puedes ayudar, el código qué me pasastes funciona perfectamente, lo que quiero ampliar es que por ejemplo el cliente de otro país que no es Colombia, a la hora de finalizar pedido y ponga como país de envío Colombia le aparezca también la restricción ( ahora mismo no aparece), aparte quiero poner también Argentina. Muchas gracias por tu ayuda. gracias!!! – Manuel Sep 03 '18 at 07:05