Skip to content

Instantly share code, notes, and snippets.

@bryceadams
Created March 25, 2015 23:18
Show Gist options
  • Save bryceadams/f118ca24437923cae6dc to your computer and use it in GitHub Desktop.
Save bryceadams/f118ca24437923cae6dc to your computer and use it in GitHub Desktop.
<?php
/**
* 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_usps_shipping_method' , 10, 2 );
function unset_usps_shipping_method( $rates, $package ) {
// Setup an array of shipping classes that do not allow UPS Shipping (@todo change this)
$shippingclass_array = array( 'no-usps' );
/**
* 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
*/
foreach( $rates as $key => $rate ) {
if (strpos($key,'usps') !== false) {
unset( $rates[$key] );
}
}
}
}
// 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