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.

Business Case

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

Solution

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 that 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
Shipping Class Slug
Shipping Class Slug

  • 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)
add-extra-cost-snippet

​​


  • 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 :
get-fedex-shipping-method-value


  • 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
fedex-ground-without-extra-cost
  • After adding $10 extra for the shipping class “HAZMAT”
fedex-ground-with-extra-cost

Plugin Compatibility

The abovementioned solution is compatible with the following shipping plugins.

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

  1. Joe Engleson
    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’?

    • PluginHive Editorial Team
      PluginHive Support says:

      Hi Joe,
      Replace line number 41 in the above code snippet with the 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.