WP SITES

3086 Coded Tutorials & 288 Plugins

Hide Shipping Methods On Cart Page Based on Weight & Shipping Class

This code enables you to only display the applicable shipping method based on the weight of the product and shipping class assigned to the product. Other shipping methods will be removed so only 1 method displays.

In this case, we use 2 shipping classes for :

  • Dry Food slug is dry-food
  • Frozen Food slug is frozen-food

The code works based on the shipping classes seen in the following screenshot :

We go to WooCommerce > Settings > Shipping and create 6 flat rate shipping methods and name them as follows :

  • Up To 10 KG Dry Food
  • Up To 10 KG Frozen Food
  • 11 – 20 KG Dry Food
  • 11 – 20 KG Frozen Food
  • 21 – 30 KG Dry Food
  • 21 – 30 KG Frozen Food

We add a fixed cost which does not change based on quantity and a quantity cost based on the shipping class assigned to the product for each method.

This is an example of a single product page shipping weight and shipping class selection.

Here’s the code you can add to your child themes functions file or custom code snippets plugin.

Note : You will need to swap out your shipping method ids in the following code to match your own.

add_filter( 'woocommerce_package_rates', 'wpsitesdotnet_woocommerce_tiered_shipping_v4', 9999, 2 );
function wpsitesdotnet_woocommerce_tiered_shipping_v4( $rates, $package ) {

    // Get the total weight of the cart
    $total_weight = WC()->cart->get_cart_contents_weight();

    // Initialize shipping class variable
    $shipping_class = '';

    // Loop through the items in the package and get the shipping class of the first item
    foreach ( $package['contents'] as $item_id => $values ) {
        $product = $values['data'];
        $shipping_class = $product->get_shipping_class(); // Get the shipping class for the product
        break; // Assuming all products in the cart have the same shipping class
    }

    // Check if the shipping class is 'dry' or 'frozen'
    $is_dry = ( $shipping_class === 'dry-food' );
    $is_frozen = ( $shipping_class === 'frozen-food' );

    // If the weight is less than or equal to 10kg
    if ( $total_weight <= 10 ) {
        if ( $is_dry ) {
            // Keep dry shipping method 40, remove others (41-46)
            unset( $rates['flat_rate:41'], $rates['flat_rate:42'], $rates['flat_rate:43'], $rates['flat_rate:44'], $rates['flat_rate:45'], $rates['flat_rate:46'] );
        } elseif ( $is_frozen ) {
            // Keep frozen shipping methods 43 and 44, remove others (40-42, 45, 46)
            unset( $rates['flat_rate:40'], $rates['flat_rate:41'], $rates['flat_rate:42'], $rates['flat_rate:44'], $rates['flat_rate:45'], $rates['flat_rate:46'] );
        }

    // If the weight is between 11kg and 20kg
    } elseif ( $total_weight > 10 && $total_weight <= 20 ) {
        if ( $is_dry ) {
            // Keep dry shipping methods 41 , remove others (40, 43-46)
            unset( $rates['flat_rate:40'], $rates['flat_rate:42'], $rates['flat_rate:43'], $rates['flat_rate:44'], $rates['flat_rate:45'], $rates['flat_rate:46'] );
        } elseif ( $is_frozen ) {
            // Keep frozen shipping methods 44, remove others (40-43, 46)
            unset( $rates['flat_rate:40'], $rates['flat_rate:41'], $rates['flat_rate:42'], $rates['flat_rate:43'], $rates['flat_rate:45'], $rates['flat_rate:46'] );
        }

    // If the weight is between 21kg and 30kg
    } elseif ( $total_weight > 20 && $total_weight <= 30 ) {
        if ( $is_dry ) {
            // Keep dry shipping method 42, remove others (40, 41, 43-46)
            unset( $rates['flat_rate:40'], $rates['flat_rate:41'], $rates['flat_rate:43'], $rates['flat_rate:44'], $rates['flat_rate:45'], $rates['flat_rate:46'] );
        } elseif ( $is_frozen ) {
            // Keep frozen shipping method 45, remove others (40-45)
            unset( $rates['flat_rate:40'], $rates['flat_rate:41'], $rates['flat_rate:42'], $rates['flat_rate:43'], $rates['flat_rate:44'], $rates['flat_rate:46'] );
        }
    }

    return $rates;
}

Instead of 6 shipping methods showing on your cart page, you only have 1.

Related Functionality

Leave a Reply

New Plugins