Show / hide shipping methods based on order weight

After adding this code snippet, you can either show or hide the required services in the cart. It shows/hides the shipping methods based on the order weight.

Add the following code to your functions.php or anywhere relevant.

If you are looking for a plugin to do this job, visit ELEX Hide Shipping Methods Plugin. Using this plugin, You can easily hide shipping methods based on various conditions.

In the above code, $order_weight stores the total weight of the order. Then it is converted to ounce (oz) and assigned to $weight_oz. If $weight_oz is less than or equal to 6 oz, then list all the services which need to be hidden in  the $shipping_services_to_hide array. If the order weight is more than 6 oz, then save flat rate in the $shipping_services_to_hide array. For existing value of $shipping_services_to_hide unset it and return $available_shipping_methods.


Comments (4)

  • Lorenzo
    Lorenzo

    Hi Gil,

    I’ve added a code on your site, Please check if that works.

    Regards,
    Lorenzo
    Team Xadapter

    July 31, 2017 at 5:12 pm
  • Gil
    Gil

    This is what I have in order to hide UPS Freight options on orders below 150 lbs, but it’s not hiding the Freight options. Please help.

    add_filter( ‘woocommerce_package_rates’, ‘show_shipping_method_on_order_weight’, 10, 2 );
    function show_shipping_method_on_order_weight( $available_shipping_methods, $package ) {
    $order_weight = 0;
    foreach( WC()->cart->cart_contents as $key => $values ) {
    $product_weight = woocommerce_get_weight($values[ ‘data’ ]->get_weight(),’lbs’);
    $quantity = $values[‘quantity’];
    if($product_weight && $quantity){
    $order_weight = $order_weight + $product_weight*$quantity;
    }
    }
    $weight_oz = $order_weight*16;
    if($weight_oz <= 2400){
    $shipping_services_to_hide = array(
    'wf_shipping_ups:308',
    'wf_shipping_ups:14',
    );
    }else{
    $shipping_services_to_hide = array(
    'wf_shipping_ups:334',
    'wf_shipping_ups:13',
    'wf_shipping_ups:01',
    'wf_shipping_ups:03',
    'wf_shipping_ups:02',
    'wf_shipping_ups:12',
    );
    }
    foreach ( $shipping_services_to_hide as &$value ) {
    unset( $available_shipping_methods[$value] );
    }
    return $available_shipping_methods;}

    July 28, 2017 at 2:01 am
  • Gil
    Gil

    This is what I have in order to hide UPS Freight options on orders below 150 lbs.

    add_filter( ‘woocommerce_package_rates’, ‘show_shipping_method_on_order_weight’, 10, 2 );

    function show_shipping_method_on_order_weight( $available_shipping_methods, $package ) {

    $order_weight = 0;
    foreach( WC()->cart->cart_contents as $key => $values ) {
    $product_weight = woocommerce_get_weight($values[ ‘data’ ]->get_weight(),’lbs’);
    $quantity = $values[‘quantity’];
    if($product_weight && $quantity){
    $order_weight = $order_weight + $product_weight*$quantity;
    }
    }
    $weight_oz = $order_weight*16;
    if($weight_oz <= 2400){
    $shipping_services_to_hide = array(
    'wf_shipping_ups:308',
    'wf_shipping_ups:14',
    );
    }else{
    $shipping_services_to_hide = array(
    'wf_shipping_ups:334',
    'wf_shipping_ups:13',
    'wf_shipping_ups:01',
    'wf_shipping_ups:03',
    'wf_shipping_ups:02',
    'wf_shipping_ups:12',
    );
    }

    foreach ( $shipping_services_to_hide as &$value ) {
    unset( $available_shipping_methods[$value] );
    }
    return $available_shipping_methods;}

    July 28, 2017 at 2:00 am
  • Gil
    Gil

    What would the shipping services be for UPS and UPS Freight? These are USPS.

    July 28, 2017 at 1:48 am

Leave a Reply