For a better experience please change your browser to CHROME, FIREFOX, OPERA or Internet Explorer.
List your Store
Conditional free shipping based on shipping class and minimal amount in Woocommerce

Conditional free shipping based on shipping class and minimal amount in Woocommerce

 To make it work first you will need:
– To add a “Free” shipping class (first),
– To enable 2 shipping methods: “Free shipping” and “Flat rate”,
– In your products with Free shipping will need to be set with the shipping class “Free”,
– In your other product will not have any defined shipping class.

1) For the “free shipping” method, you will not add any restrictions amount to it.

2) For the “Flat rate” shipping method, you will set it as in this screen shot:

3) The magic will be done by the following code that will make the rest:

add_filter('woocommerce_package_rates', 'conditional_free_shipping', 10, 2);
function conditional_free_shipping( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    ## -- Your settings bellow -- ##

    $shipping_class  = 'free'; // "Free" shipping class products
    $min_free_amount = 200;    // Minimal Free shipping amount for normal products

    ## -- -- -- -- -- -- -- -- -- ##

    $has_normal = $has_free = false; // Initializing
    $products_total = 0; // Initializing

    // Loop through cart items
    foreach( $package['contents'] as $cart_item ) {
        if( $cart_item['data']->get_shipping_class() == $shipping_class ) {
            $has_free = true;
        } else {
            $has_normal = true;
            // Get the total purchased amount for normal product
            $products_total += $cart_item['line_total'] + $cart_item['line_tax'];
        }
    }

    foreach ( $rates as $rate_key => $rate ){
        // 1. Only Free shipping products in cart
        if( $has_free && ! $has_normal ) {
            if( 'flat_rate' === $rate->method_id )
                unset( $rates[$rate_key] ); // Remove flat rate



        }

        elseif(( $has_free && $has_normal )){
             if( 'flat_rate' === $rate->method_id && $products_total <= $min_free_amount )
                unset( $rates[$rate_key] );
                }


        // 2. Only normal products in cart OR Both products kind in cart
        elseif( ( ! $has_free && $has_normal )  ) {
            // A. If it's under the min amount
            if( 'free_shipping' === $rate->method_id && $products_total < $min_free_amount )
                unset( $rates[$rate_key] ); // Remove Free shipping
            // B. When min amount is reached
            elseif( 'flat_rate' === $rate->method_id && $products_total >= $min_free_amount )
                unset( $rates[$rate_key] ); // Remove flat rate
        }
    }
    return $rates;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

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

leave your comment


Your email address will not be published. Required fields are marked *

Top