Woocommerce Shipping Plugin – Hide One Shipping Method if Order is Over Certain Weight, and Show Alternate Option

This is a business case from a customer using our ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label. He wants to hide DHL when the order weight reach certain limit, and show some other shipping options or even free shipping, if the order weight is over the limit. The reason the shop owners want to offer a different shipping option or free shipping is because after certain weight the shipping cost is much cheaper and to attract the customers with a cheaper or free shipping option is a fair deal. Most shipping plugins by default don’t have the option to hide its shipping options in cart, unless you use some other third party plugin to achieve that or use a code snippet. In this article we’ll discuss how we can achieve this and what are the requirements.

This same solution can be used for our other shipping plugins such as:

But in this article we’ll take an example of our plugin ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label and offer free shipping as the only option when order weight is over a particular weight.

Requirements:

Set up WooCommerce Free Shipping Method

You need to add a shipping zone in WooCommerce Shipping Shipping Zones, and name it ‘everywhere’. Leave the country selection blank so that it will apply to all countries. Add a Free Shipping method. This option will be available for all countries. Make sure you go to cart page and check if the free shipping option is showing fine.

Set Up and Configure the ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label

You need to purchase the plugin from our site, and set up the plugin to get the shipping rates. To set up the DHL plugin and retrieve the shipping rates, you need to get the API credentials from DHL Express team. You need to open an account and register in https://xmlportal.dhl.com and DHL will provide the Test and Live API credentials. Depending on which set of credentials you received you need to enable/disable the ‘Show DHL Account Rates‘ option under the Rates and services tab in the plugin settings. For Test credentials you need to disable the ‘Show DHL Account Rates‘ option, and for live credentials you need to enable the ‘Show DHL Account Rates’ option.

Make sure all your products (simple and variations) have weight and dimensions in them. You can add weight and dimension while editing the product. Go to shipping tab under the product data and fill up the fields ‘Weight‘,’Length‘,’Width‘ and ‘Height/Thickness‘. The plugin requires these data to calculate shipping and the packing option in the plugin settings. Once done, enable the DHL Express services in the plugin settings under the ‘Rates and Services Tab’ with the product code ‘N’ for Express Domestic, ‘P’ for Express Worldwide, ‘D’ also Express Worldwide (doc type) and U also Express Worldwide (doc type). Different countries require different services, and so depending on the requirement the API will automatically return rates for one of the above services. Now, check if you’re getting the DHL shipping options in the Cart page. If you’re not getting any shipping rates, please make sure you have the address correct or contact our support team.

Now on the cart page, both Free shipping and DHL option should be available for all countries. 

Configure and Add the Code snippet to Show/Hide Shipping Method Based on Order Weight

To install the code snippet on your site, first install the code snippet plugin by Shea Bunge – Download Link. Depending on which plugin you’re using you’ll have to configure the code snippet. Enter the shipping ID to the array which you want to hide. In the current code snippet I’ve entered wf_dhl_shipping to hide DHL and entered free_shipping to show free shipping when order weight is over the limit, and also enter the weight in the code snippet is in ounce so I’ve given as 176.37oz- weight equivalent to 5 kg.

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 >= 176.37){       // Configure the weight in ounce here.
$shipping_services_to_hide = array(     
"wf_dhl_shipping"  //Configure the shipping ID here to hide if weight is below the limit.
);
}else{
$shipping_services_to_hide = array('free_shipping');  //Configure the shipping ID when weight is over the limit.
}

foreach ( $shipping_services_to_hide as $key => $value ) {
unset( $available_shipping_methods[$value] );
}
return $available_shipping_methods;
}

The code snippet will hide DHL shipping option in general since wf_dhl_shipping applies to all DHL services if weight is below 5 kg, if you want to hide specific service in DHL you’ll have to get the shipping ID with the product code. And if the weight is over 5 kg it will show the free shipping option. If you want to set some other shipping option when weight is over the limit you can configure the shipping ID in the code snippet itself. But you’ll have to define the plugin or shipping method to show in the cart first.

How will I know the Shipping ID I need to enter?

To get the shipping ID of the services, showing in the cart or checkout page, you can right click the shipping option in the Cart page and select Inspect. And in the console that shows up, the element details will be already selected so you can see the shipping ID under “value”. This you can copy and enter the same in the Show Hide shipping code snippet in the shipping ID field.
Hope this article helped with your business requirement. If you need help setting up the plugin and the code snippet you can always contact our team.

To explore more details about the shipping plugins, please check out the product pages.

You can also check out other WooCommerce and WordPress plugins in ELEX.

    Leave a Reply