Add an Additional Cost to the Shipping Service based on WooCommerce Shipping Class
In this article, we will check out how you can add an additional cost to a particular shipping service for a particular product. We will cover a customized solution based on the WooCommerce shipping classes which will add an amount to the live shipping rates if the cart contains any product related to a particular shipping class.
Mark is a WooCommerce store owner who deals with Hazardous Products and ships them to his customers.
According to Mark, “Can you give me a way to add a fee based on the product’s Shipping Class in addition to the FedEx Ground or FedEx Home Delivery charges?
Is this possible? For example, if I have a product with the Shipping Class “HAZMAT” I want to add $10 to the actual shipping rate returned by FedEx“
Add the customized solution to your functions.php or anywhere relevant.
In the code given in the link:
- $shipping_class contains the slug of shipping class(es) for which you want to add an additional cost for shipping.
- $extra_cost contains an additional cost which is to be added to shipping.
- $shipping_services contains IDs of services for which you want to add an additional cost for shipping.
If $shipping_class matches with the shipping class of the cart product, then $shipping_class_exists becomes true. If $shipping_class_exists exists, then the cost of all the listed services is added with $extra_cost and saved in $available_shipping_methods. The Function returns $available_shipping_methods which contains an additional cost of shipping.
- To locate the shipping class slug, please visit the shipping class section WooCommerce => Settings => Shipping => Shipping class
- Assign the value of slug to the array
- To add the extra charges you need to enter the additional amount under the $extra_cost
- Then replace ‘wf_shipping_ups:01’ in the code snippet with the value of the appropriate shipping method as shown in the screenshot below. (for example, I have added $10 extra to FedEx Ground)
- To find the value for FedEx Ground, inspect it and provide exactly what is assigned to the variable “value” as shown in the screenshot below :
- Once done, you will now be able to see additional charges that will be added on top of the normal shipping rates on the cart page as shown in the screenshots below:
- Without adding extra charges
- After adding $10 extra for the shipping class “HAZMAT”
The abovementioned solution is compatible with the following shipping plugins.