Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogists/b8a4f2c13eef89d63ce936e72094e73e to your computer and use it in GitHub Desktop.
Save woogists/b8a4f2c13eef89d63ce936e72094e73e to your computer and use it in GitHub Desktop.
[UPS Shipping Method] remove UPS as a shipping option for specific shipping classes
/**
* Check the cart for specific classes, remove UPS Shipping method if they are present
*
* REMOVE THE TOP <?php if there is no ?> before (or you have an error after adding this)
*
* Add the code to your theme functions.php file
*/
add_filter( 'woocommerce_package_rates', 'unset_ups_shipping_method' , 10, 2 );
function unset_ups_shipping_method( $rates, $package ) {
// Setup an array of shipping classes that do not allow UPS Shipping (@todo change this)
$shippingclass_array = array( 'no-ups' );
/**
* 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 ) {
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_array ) ) {
/**
* Unset the shipping methods here
*/
unset( $rates['ups:01'] );
unset( $rates['ups:02'] );
unset( $rates['ups:03'] );
unset( $rates['ups:04'] );
unset( $rates['ups:05'] );
unset( $rates['ups:06'] );
unset( $rates['ups:07'] );
unset( $rates['ups:08'] );
unset( $rates['ups:09'] );
unset( $rates['ups:10'] );
unset( $rates['ups:11'] );
unset( $rates['ups:12'] );
unset( $rates['ups:13'] );
unset( $rates['ups:14'] );
unset( $rates['ups:15'] );
// 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