Code snippet(based on States) to adjust the base cost of one of the shipping classes if both of them come up in the cart .Besty
The following code snippet(based on States) can be used to adjust the base cost of one of the shipping classes if both of them come up in the cart.
Note : This snippet only functions with our Shipping Pro plugin.
Kindly copy this snippet into Appearance –> Editor –> functions.php of your WordPress theme
In the above snippet, the following three changes need to be made.
1. Include state codes for which you want the subtraction of base cost to happen.
In the above snippet, the only state added is ‘CA’ (California). You can add more states inside the bracket in the following manner – ‘CA’,’FL’,’GA’.
2. Include the shipping class for which you want the adjustment to happen.
Here in the snippet we have included “shipping-class-a” for which the prices will be adjusted.
You can include the shipping class name (based on your requirement).
3. The adjustment value. In the snippet above, we want to reduce the cost by 13 so we have put a value of 13 as shown below :
$costs[$method_group][‘cost’][$shipping_group] -= 13;
If you wish to, you can add a value as well as shown :
$costs[$method_group][‘cost’][$shipping_group] = 13;
So the above snippet will function like this:
Suppose we have created two sets of rules for two shipping classes(shipping-class-a and shipping-class-b) as shown :
-If the two items are placed in cart. One is of shipping class a (shipping-class-a), then 13 will be subtracted from their combined shipping cost.
So the final rate will come as (20+7 -13 = 14) as shown in cart :