What can we help you with?
Offer Free Shipping, Flat Rate Shipping and ELEX Shipping plugin Options For Different WooCommerce Products
I got a query from a customer who had various artists selling their products on a multi-vendor site. He wanted to set different shipping options based on the products or the painting they purchase. Some products will have only free shipping, and some will have flat rate shipping, and for some products he wants the plugin to calculate the shipping cost based on weight, dimension and destination address. Currently the customer’s query is for ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label , but its applicable to our other shipping plugins as well. Right now the problem he is facing is, if he enables flat rate shipping, free shipping and also DHL option, then the customer gets all options on the Cart page.That means the customer will finally choose the free shipping option. So, based on the product added to the cart, he wants to hide the other shipping options so that only the shipping option linked to that product will show up in cart.
Here’s a screenshot of how the cart actually looks with all the shipping methods enabled.
In this article, I’ll explain how this business case can be achieved and what are the plugins or other set up required in WooCommerce for this solution. I’ll take the DHL plugin for example, but if you’re using any of the ELEX owned shipping plugins, this solution will work fine.
Requirements:
Shipping Plugin
Since we are taking DHL plugin as an example in this article, you can purchase the DHL plugin from the following link to get the DHL services in cart and checkout page – https://elextensions.com/plugin/woocommerce-dhl-express-ecommerce-paket-shipping-plugin-with-print-label/ However, if you wish to use any other ELEX shipping plugins, you can purchase them from the following link – https://elextensions.com/product-category/shipping/
To set up the DHL plugin and retrieve the shipping rates, you need the API credentials from DHL team. For DHL Express, you need to open account and register in https://xmlportal.dhl.com and DHL will provide the Test and Live API credentials. Please note, for this Business case, only DHL Express module will work since the other modules of DHL does not support Rate Request options. Once set up is complete, check if you’re getting the shipping rates in the cart and checkout page. For detailed set up guide you can check our documentation here.
Shipping Class
You need to set up the shipping classes, say free-shipping, flat-rate-shipping and DHL-shipping and assign the products to those shipping classes accordingly. Define a shipping class in WooCommerce > Shipping > Shipping class. Add the shipping classes as in the screenshot below:
Once you’ve added the shipping classes, you need to assign these classes to the products. To add the shipping class to the products you need edit the product and under the shipping tab you can choose the shipping class from the drop-down. For products you want to offer free shipping, select the shipping class – Free shipping, and for the products that need Flat rate shipping- select the shipping class Flat rate shipping. And if you want to offer different type of flat rate shipping rates you can add more flat rate shipping classes. For products that need DHL shipping rates, select the DHL shipping class from the drop down. The below image shows the option where you can choose the shipping class in the edit product page.
Woocommerce Shipping zones
Set Up the Woocommerce shipping zones so that you can add the Free Shipping method as well as the Flat Rate shipping method. Define a shipping zone in WooCommerce > Shipping > Shipping zone. If you want the same options for all countries you ship to then you can name the shipping zone as ‘Everywhere’ and leave the country selection as blank. Once the zone is created, add the free and flat rate shipping methods. You can click on Edit Zone > Add shipping method, and select free shipping from the drop down and click the Add shipping method button.You can add the Flat Rate shipping method as well in the WooCommerce shipping zones. Click the Add shipping method button and select Flat Rate from drop down and click the Add shipping method again. To add the shipping rate to the Flat Rate shipping option, you can click the Edit option, and then add the shipping rates. Also you can select the shipping class to which it applies to. So in this example I’ve added a flat rate fee of 15$ for the shipping class Flat rate shipping.
If you add more shipping classes for flat rate you’ll have the option to add more flat rate while editing the flat rate shipping option. So if some products need Flat Rate shipping of 15$ and few others need a flat rate of 20$ you can add another flat rate shipping class with some other name and assign a fixed rate in the edit options. And if you want to increase the shipping cost when more items are added, you can use an advanced flat rate fee option, by defining the fee as 15 + ( 5 * [qty] ).
This will charge 15$ as based cost and 5$ per quantity. So, if there are 2 items, the flat rate fee will be 25$.
Show Hide Shipping options
You can use this plugin to show/hide shipping options based on the shipping class and zone. You can download the plugin from the following link – https://elextensions.com/wp-content/uploads/2018/11/xa-show-hide-shipping-methods-1.zip
Configure the plugin to hide shipping options based on the Shipping class and Zone. For free shipping class, hide ‘flat_rate’ and ‘wf_dhl_shipping’ shipping IDs, and for Flat rate shipping class, hide ‘free_shipping’ and ‘wf_dhl_shipping’ shipping IDs. For DHL shipping class, hide ‘free_shipping’ and ‘flat_rate’ shipping IDs as shown below.
With the above settings, the shipping options will hide according to the products present in cart. But, if you have multiple products from different shipping classes in the cart page, it would hide all shipping options. If you have only one item in the cart, this will work without any issues. For multiple shipping class products in cart, you’ll need to add the below code snippet into the functions.php in theme folder. or add the code snippet plugin by installing the code snippet plugin and add the code there. Please make sure you replace the class slug name correctly in the code snippet.
Code Snippet:
add_filter( 'woocommerce_cart_shipping_packages', 'wf_split_cart_by_shipping_class_group' ); function wf_split_cart_by_shipping_class_group($packages){ //Reset packages $packages = array(); //Init splitted package $splitted_packages = array(); // Group of shipping class ids $class_groups = array( 'group1' => array('dhl-shipping'), 'group2' => array('flat-rate-shipping'), 'group3' => array('free-shipping') ); foreach ( WC()->cart->get_cart() as $item_key => $item ) { if ( $item['data']->needs_shipping() ) { $belongs_to_class_group = 'none'; $item_ship_class_id = $item['data']->get_shipping_class(); if($item_ship_class_id){ foreach($class_groups as $class_group_key => $class_group){ if(in_array($item_ship_class_id, $class_group)){ $belongs_to_class_group = $class_group_key; continue; } } } $splitted_packages[$belongs_to_class_group][$item_key] = $item; } } // Add grouped items as packages if(is_array($splitted_packages)){ foreach($splitted_packages as $splitted_package_items){ $packages[] = array( 'contents' => $splitted_package_items, 'contents_cost' => array_sum( wp_list_pluck( $splitted_package_items, 'line_total' ) ), 'applied_coupons' => WC()->cart->get_applied_coupons(), 'user' => array( 'ID' => get_current_user_id(), ), 'destination' => array( 'country' => WC()->customer->get_shipping_country(), 'state' => WC()->customer->get_shipping_state(), 'postcode' => WC()->customer->get_shipping_postcode(), 'city' => WC()->customer->get_shipping_city(), 'address' => WC()->customer->get_shipping_address(), 'address_2' => WC()->customer->get_shipping_address_2() ) ); } } return $packages; }
With this solution, you’ll get a split cart option by shipping class and each class will have the option for the shipping. See below screenshot:
In the above screenshot, I’ve assigned the shipping class ‘flat-rate-shipping’ to the product ‘Smart Watch’ and assigned the shipping class ‘DHL-shipping’ to the product ‘Mobile’, Now the cart calculates separate shipping for the class ‘flat rate shipping’ and separate shipping for the class ‘DHL shipping’. Now you can compare how the shipping methods display after configuring the code and the plugins.
This solution will work for all our other shipping plugins as well. Just make sure you copy the right shipping ID while configuring the Show Hide Shipping Method Plugin.
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 already been selected so you can see the shipping ID under the “value”. This you can copy and enter the same in the Show Hide shipping method in the shipping ID field.
If you’re using Multi-vendor plugin such as Dokan you’ll need to install an ad-don to link Dokan with our DHL plugin you can download the plugin here. Hope this article helped with your business case, do let us know if you have any queries. If you face any issues do contact our support here.
To explore more details about the shipping plugins, please check out the product pages.
- ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label,
- ELEX WooCommerce Australia Post Shipping Plugin with Print Label & Tracking,
- ELEX WooCommerce USPS Shipping Plugin with Print Label,
- ELEX Stamps.com Shipping Plugin with USPS Postage for WooCommerce,
- ELEX EasyPost (FedEx, UPS & USPS) Shipping & Label Printing Plugin for WooCommerce
You can also check out other WooCommerce and WordPress plugins in ELEX.
2 Comments. Leave new
Can you confirm that the code is still correct?
I am using WCFM and am getting the following error when loading two items with different shipping classes assigned. (I only have 2, free shipping and easypay). https://imgur.com/a/9rqBGJQ
When only one item is in the cart both options are working well with the other option hidden.
Hello,
Thanks for reaching out to us.
The above code snippet is used for splitting the cart based on shipping class and showing different rates. However, If you are already using a third-party multi-vendor plugin then there is no need to use the code snippet as the vendor plugin will split the cart based on the vendor’s store and show the rates. If you are not getting the rates then it might be related to the vendor’s store or product configuration.