Search Knowledge Base by Keyword

Add an additional cost to shipping service based on Shipping Class

The code snippet facilitates you to add an additional shipping cost to the required service(s) for products which belong to the specific class(es).

Add the following code to your functions.php or anywhere relevant.

In the above code:

– $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.  

Last Updated On September 24, 2018

2 thoughts on “Add an additional cost to 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.