Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.