add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
//HERE set the product category in the array (ID, slug or name)
$terms = array( 'rubber-gloves' );
$taxonomy = 'product_cat';
$catTotalCost = 0;
//Loop through cart items checking for defined product IDs
foreach( $package['contents'] as $cart_item ) {
if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ){
$quantity = $cart_item['quantity'];
$price = $cart_item['data']->get_price();
$itemTotalCost = $quantity * $price;
$catTotalCost += $itemTotalCost;
}
}
foreach( $rates as $rate_id => $rate ) {
if($catTotalCost > 0 && $catTotalCost < 250){
unset( $rates['free_shipping:3'] );
}else{
unset( $rates['flat_rate:4'] );
}
}
return $rates;
}