5

With WooCommerce, I need to have Free Shipping over certain amount of 250 except the heavy products that are included in the cart.

Does anyone know what i should do?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
spy
  • 85
  • 1
  • 10
  • So you want free shipping for some items in the cart but not for others? – Daniel_ZA Feb 17 '17 at 13:40
  • 1
    please edit your question according to [mcve] – Charles-Antoine Fournel Feb 17 '17 at 13:52
  • hey there yes kinda.I need the free shipping only for the light products over 250 . I need to have the heavy products with shipping costs over the 250 limit. – spy Feb 17 '17 at 15:12
  • Look at my answer here: http://stackoverflow.com/questions/15457059/woocommerce-create-free-shipping-for-single-item-while-using-ups-extension/34282227#34282227 . Not exactly the same, but very similar .. should help point you in the right direction – Daniel_ZA Feb 20 '17 at 05:26

2 Answers2

3

This custom code will keep free shipping method and will hide other shipping methods when the cart amount is up to 250 and if products are not heavy (less than 20 kg here)… To not allow Free shipping for orders less than 250, you can set this in woocommerce (see at the end).

First you will have to sure that the weight is set in each heavy product (for simple or variables products (in each variations). The cart subtotal here is Excluding taxes (and you can change it to Including taxes easily).

enter image description here

Then here is that custom hooked function in woocommerce_package_rates filter hook:

add_filter( 'woocommerce_package_rates', 'conditionally_hide_other_shipping_based_on_items_weight', 100, 1 );
function conditionally_hide_other_shipping_based_on_items_weight( $rates ) {

    // Set HERE your targeted weight (here is 20 kg) <== <== <== <== <==
    $targeted_product_weight = 20;
    
    // Set HERE your targeted cart amount (here is 250)  <== <== <== <== <==
    $targeted_cart_amount = 250;
    // For cart subtotal amount EXCLUDING TAXES
    $passed = WC()->cart->subtotal_ex_tax >= $targeted_cart_amount ? true : false;
    // For cart subtotal amount INCLUDING TAXES (replace by this):
    // $passed = WC()->cart->subtotal >= $targeted_cart_amount ? true : false;
    $light_products_only = true; // Initializing

    // Iterating trough cart items to get the weight for each item
    foreach( $package['contents'] as $cart_item ){
        // Getting the product weight
        $product_weight = $cart_item['data']->get_weight();

        if( !empty($product_weight) && $product_weight >= $targeted_product_weight ){
            $light_products_only = false;
            break;
        }
    }

    // If 'free_shipping' method is available and if products are not heavy
    // and cart amout up to the target limit, we hide other methods
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id && $passed && $light_products_only ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }

    return ! empty( $free ) ? $free : $rates;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and it works for simple and variable products…

You Will also have to in WooCommerce settings > Shipping, for each shipping zone and for the "Free Shipping" method your minimum order amount:

enter image description here

You will need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in woocommerce shipping settings.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you so much for your time!! Will this give only the heavy items a per/kg shipping cost over the 250 while the "light" items that are included in the cart will be free shipping? – spy Feb 17 '17 at 16:43
  • i dont understand i think sorry for that...so what will happen with the heavy items that are on the cart with the light ones?Will they have a shipping cost as i want? – spy Feb 17 '17 at 16:54
  • Does this still work in 2022? I tried this code and I am having a strange issue, I am not able to add products to the cart. When I remove the code then I am able to add products to the cart. – BigDropGR Aug 05 '22 at 09:41
0

For free shipping over certain amount, you can use inbuilt WooCommerce option.

For skipping free shipping if for some specific products , you can use below snippet.

 add_filter('woocommerce_package_rates', 'hide_shipping_method_if_particular_product_available_in_cart', 10, 2);

        function hide_shipping_method_if_particular_product_available_in_cart($available_shipping_methods)
        {
            global $woocommerce;

            // products_array should be filled with all the products ids
            // for which shipping method (stamps) to be restricted.

            $products_array = array(
                101,
                102,
                103,
                104
            );

            // You can find the shipping service codes by doing inspect element using
            // developer tools of chrome. Code for each shipping service can be obtained by
            // checking 'value' of shipping option.

            $shipping_services_to_hide = array(
                'free_shipping',

            );

            // Get all products from the cart.

            $products = $woocommerce->cart->get_cart();

            // Crawl through each items in the cart.

            foreach($products as $key => $item) {

                // If any product id from the array is present in the cart,
                // unset all shipping method services part of shipping_services_to_hide array.

                if (in_array($item['product_id'], $products_array)) {
                    foreach($shipping_services_to_hide as & $value) {
                        unset($available_shipping_methods[$value]);
                    }

                    break;
                }
            }

            // return updated available_shipping_methods;
    return
        }
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • hey there and thanks for the snippet.I need the free shipping only for the light products over 250 . I need to have the heavy products with shipping costs over the 250 limit. Is that snippet including that?Thanks so much – spy Feb 17 '17 at 15:08
  • free shipping for certain minimum amount can set via Woocommerce. – mujuonly Feb 17 '17 at 15:13
  • Heavy product ids can be added into snippet and can manage the shipping with slght modificatio – mujuonly Feb 17 '17 at 15:14
  • $products_array = array( 101, 102, 103, 104 you mean that one right?So adding the ids here the heavy items will only have shipping costs over 250 and the light ones will be free after i set in the wc?sorry if that sounds stupid – spy Feb 17 '17 at 15:17