Created
October 4, 2015 00:20
-
-
Save helgatheviking/4db6ac3021287e4314db to your computer and use it in GitHub Desktop.
Remove certain items from shipping calculations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function remove_free_shipping_items( $packages ) { | |
foreach( $packages as $i => $package ){ | |
foreach ( $package['contents'] as $key => $item ) { | |
if ( $item['data']->get_shipping_class() == 'free' ) { | |
unset( $packages[$i]['contents'][$key] ); | |
add_filter( 'woocommerce_cart_needs_shipping', '__return_true' ); | |
} | |
} | |
} | |
return $packages; | |
} | |
add_filter( 'woocommerce_cart_shipping_packages', 'remove_free_shipping_items' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great, thanks! One issue I see is that if the user only buys the free shipping items, they get the "There are no shipping methods available." notice, which is not ideal. I want to preserve that notice if there is actually a shipping problem (bad postal code, out of area, etc), but otherwise have something like "No charge for shipping on this item!" Is that easy enough to do?