Search Knowledge Base by Keyword

Add an Additional Cost to the Shipping Service based on 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. This article will cover a customized solution based on the shipping classes which will add an amount to the live shipping rates if the cart contains any product related to a particular shipping class.

Business Case…

Mark is a WooCommerce store owner who deals with Hazardous Products and ships it 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 that 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…

Solution…

Add the customized solution to your functions.php or anywhere relevant.

In the code given in the link:

  • $shipping_class_ids contains the IDs 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_ids matches with the shipping class ID 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 id, please inspect the shipping class and you will locate the variable “data-id”.


  • Assign the value of “data-id” to the array as shown in the screenshot below (in my case, data-id is 37, hence I have assigned 37 to the array)


  • To add the extra charges you need to 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” :

Plugin Compatibility

The abovementioned solution is compatible with the following shipping plugins.

Last Updated On January 28, 2019

2 thoughts on “Add an Additional Cost to the Shipping Service based on Shipping Class

  1. Joe Engleson says:

    This works perfectly, but we don’t want the additional charge to be added to the Local Pickup shipment method. How do we tweak this code to NOT add the additional charge if the available_shipping_method is ‘Local Pickup’?

    • XAdapter Info says:

      Hi Joe,
      Replace line number 41 in the above code snippet with below code to avoid adding extra charges to ‘Local Pickup’.

      if($value->method_id != ‘local_pickup’) {
      $available_shipping_methods[$key]->cost += $extra_cost;
      }

Comments are closed.