Skip to content
Advertisement

Exclude WooCommerce virtual product from counting towards free delivery

On my WordPress/WooCommerce website we offer free delivery for all orders over 500 DKK. This is working perfectly fine. We have made a giftcard as a virtual product, seeing as we sent all of our products digitally. The problem is for some reason the virtual product counts towards this 500 DKK free delivery limit.

What we’re trying to do is exclude all virtual products from counting towards the free delivery limit.

I have no idea how to go about this. Our website is: http://detitalienskekoekken.dk.linux99.unoeuro-server.com.

The way it is set up is we have a plugin that offers various delivery methods. One of these methods (delivery to a shop) is set to cost 49 DKK but upon reaching 500 DKK you get the delivery for free on this particular option.

I haven’t been able to find a code for this online, so I am sorry for not providing this.

Advertisement

Answer

Update for your specific plugin:

add_filter('woocommerce_package_rates', 'custom_free_shipping_option', 15, 2 );
function custom_free_shipping_option($rates, $package){

    // HERE set the "minimum order amount" for free shipping
    $limit = 500;

    $free_total = 0;

    // Get the cart content total excluding virtual products
    foreach( WC()->cart->get_cart() as $cart_item )
        if( ! $cart_item['data']->is_virtual( ) )
            $free_total += $cart_item['line_total'];

    // Set the cost to 0 based on specific cart content total
    if( $free_total > $limit )
        foreach ( $rates as $rate_key => $rate )
            if( 'pakkelabels_shipping_gls' === $rate->method_id )
                $rates[$rate->id]->cost = 0;

    return $rates;
}

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

It should work with your specific shipping method.


Original answer (for classic “Free Shipping” method):

Here is the necessary code that will replace the shipping method Free delivery “minimum order amount” option excluding virtual products:

add_filter('woocommerce_package_rates', 'custom_free_shipping_option', 10, 2 );
function custom_free_shipping_option($rates, $package){

    // HERE set the "minimum order amount" for free shipping
    $limit = 500;

    $free_total = 0;

    // Get the cart content total excluding virtual products
    foreach( WC()->cart->get_cart() as $cart_item )
        if( ! $cart_item['data']->is_virtual( ) )
            $free_total += $cart_item['line_total'];

    // Disabling free shipping method based on specific cart content total
    if( $free_total < $limit )
        foreach ( $rates as $rate_key => $rate )
            if( 'free_shipping' == $rate->method_id )
                unset( $rates[ $rate_key ] );

    return $rates;
}

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

Tested and working

So you can even remove this option in the Free delivery settings.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement