Free shipping depending on weight and minimum amount

With WooCommerce, I need to have free shipping for a specific quantity of 250 , with the exception of the heavy products that are included in the cart.

Does anyone know what I should do?

Thanks.

+5
source share
2 answers

This custom code will support the free shipping method and will hide other delivery methods if the cart is more than 250 , and if the goods are not heavy (less than 20 kg here) ... To prevent free shipping for orders less than 250, you can set this in woocommerce (see end).

First you will need to make sure that the weight is set in each heavy product (for simple or variable products (in each option). Percentage of the basket Excluding taxes (and you can change it including taxes easily.)

enter image description here

Then this custom function got caught in the woocommerce_package_rates filter:

 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) <== <== <== <== <== $target_product_weight = 20; // Set HERE your targeted cart amount (here is 250) <== <== <== <== <== $target_cart_amount = 250; // For cart subtotal amount EXCLUDING TAXES WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ; // For cart subtotal amount INCLUDING TAXES (replace by this): // WC()->cart->subtotal >= $target_cart_amount ? $passed = true : $passed = false ; // Iterating trough cart items to get the weight for each item foreach(WC()->cart->get_cart() as $cart_item){ if( $cart_item['variation_id'] > 0) $item_id = $cart_item['variation_id']; else $item_id = $cart_item['product_id']; // Getting the product weight $product_weight = get_post_meta( $item_id , '_weight', true); if( !empty($product_weight) && $product_weight >= $target_cart_amount ){ $light_products_only = false; break; } else $light_products_only = true; } // 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; } 

The code goes in the function.php file of your active child theme (or theme), as well as in any plug-in file.

This code has been tested and works for simple and variable products ...

You will also need to specify the minimum order amount in the WooCommerce> Delivery settings for each delivery zone and for the "Free Shipping" method:

enter image description here

You need to update the cached delivery data: disable, save and enable, save the associated delivery methods for the current delivery zone in the woocommerce delivery settings.

+3
source

For free delivery for a certain amount, you can use the built-in WooCommerce option.

To skip free shipping, if for some specific products you can use the snippet below.

  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 } 
0
source

Source: https://habr.com/ru/post/1264346/


All Articles