Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Created November 14, 2014 07:17
Show Gist options
  • Save ChromeOrange/9f0870cf034de287bbb4 to your computer and use it in GitHub Desktop.
Save ChromeOrange/9f0870cf034de287bbb4 to your computer and use it in GitHub Desktop.
Unset WooCommerce Free Shipping if certain products are in the cart
/**
* Check the cart for specific products, remove Free Shipping method if they are present
*
* Add the code to your theme functions.php file
*/
add_filter( 'woocommerce_package_rates', 'unset_free_shipping_method' , 10, 2 );
function unset_free_shipping_method( $rates, $package ) {
/**
* Setup an array or products
*
* eg $removeshipping_array = array( '7246' );
* or $removeshipping_array = array( '7246','7500','4567' );
*/
$removeshipping_array = array( '916' );
/**
* loop through the cart looking for the products in the array above
* and unset the Free shipping methods as necessary
*/
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
if( in_array( $values['product_id'], $removeshipping_array ) ) {
/**
* Unset the shipping methods here
*/
unset( $rates['free_shipping'] );
// The rates have been removed, no point in carrying on
break;
}
}
// Return what's left of the $rates array
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment