I need to unset a WooCommerce shipping rate, and have come up with this function too loop through the products in the order and add up their weight. I need to unset the UPS option if the weight is greater than 50 lbs:
add_filter( 'coocommerce_package_rates', 'hide_ups_over_50', 10, 2 );
function hide_ups_over_50( $rates, $package ) {
$totalWt = 0;
$items = $package[contents];
foreach ($items as $item) {
$quantities[] = $item[quantity];
if ( strlen( $item[variation_id] ) ) {
$prodIDs[] = $item[variation_id];
} else {
$prodIDs[] = $item[product_id];
}
$productMeta = get_post_meta($item[product_id]);
$itemWt = $productMeta['_weight'][0];
$totalWt = $totalWt + ($itemWt * $item['quantity'] );
}
if( $totalWt > 50 ) {
unset( $rates['ups:03'] );
}
// Return what's left of the $rates array
return $rates;
}
I have tried putting it in the functions.php file of my theme, and also just using the calculations and the unset() call inside of a plugin I wrote that calculates freight shipping costs. Either way, it doesn't unset the ups:03 shipping rate. I'd rather not edit the UPS plugin if it can be avoided. Where should this function be called?