I have been looking for a way to condtionally disable two shipping methods
- table rate
- distance rate
Based on the item count.
By item count I do not mean quantity, I mean how many different products are in the cart. IE 2 Lamps and 3 tables in the cart would be an item count of 2 and a combined quantity of 5.
I would also like to ensure this rule is in effect for a certain category only.
I tried:
function hide_shipping_count_based( $rates, $package ) {
// Set count variable
$cart_count = 0;
// Calculate cart's total
foreach( WC()->cart->cart_contents as $key => $value) {
$cart_count ++;
}
// only if the weight is over 150lbs find / remove specific carrier
if( $cart_count > 2 ) {
// loop through all of the available rates
unset( $rates[ 'distance_rate' ] );
unset( $rates[ 'table_rate' ] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_count_based', 10, 2 );