Hide a Shipping Method Above a Weight
Removes a light-parcel option, such as letter post or a small flat rate, when the order is heavier than that service allows.
Opens WPIDE on
Code
$wpide_weight_limit = 2; // Maximum weight in your store's unit.
$wpide_weight_method = 'flat_rate:5'; // Method ID or rate ID to hide.
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) use ( $wpide_weight_limit, $wpide_weight_method ) {
$weight = 0.0;
foreach ( (array) $package['contents'] as $item ) {
$weight += (float) $item['data']->get_weight() * (int) $item['quantity'];
}
if ( $weight <= $wpide_weight_limit ) {
return $rates;
}
foreach ( $rates as $id => $rate ) {
if ( $id === $wpide_weight_method || $rate->get_method_id() === $wpide_weight_method ) {
unset( $rates[ $id ] );
}
}
return $rates;
}, 20, 2 );
Notes
Use a method ID (flat_rate, free_shipping) to hide every instance or a full rate ID such as flat_rate:5 for one.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.