Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created October 4, 2015 00:20
Show Gist options
  • Save helgatheviking/4db6ac3021287e4314db to your computer and use it in GitHub Desktop.
Save helgatheviking/4db6ac3021287e4314db to your computer and use it in GitHub Desktop.
Remove certain items from shipping calculations
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' );
@surfzen
Copy link

surfzen commented Sep 6, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment