Using Calculation Rules to Add Per-Item Shipping in Commerce Shipping

Date Published: 

In this screencast I demonstrate how to do shipping charges per item in the cart. It shows using a loop in Rules and how to do something new and different with shipping.

We want to add $5.00 to the shipping charge for every item they have. So if the quantity is 5 for some line item, we'll add $25 to the shipping charge.

The technique is this:

* Add a shipping service. We're using a flat rate service with a $10 base rate.
* Add a new "Calculation rule" for that service.
* Add an action loop that loops through the line items in the order.
* Add an action "Calculate a value" and multiply the quantity times 500 ($5.00) and put the result in a named variable.
* Add an action "Add an amount to the unit price" that adds the named variable to the unit price of the shipping line item.


kaido24 on May 26, 2013

Wow that was so helpful.. I just forgot one little detail (tried to change or add some value to price and chose Price component type: base price)
) and couldn't get my rule to work but after looking this video I found my detail. By the way I changed flat rate service price conditionally rather creating free shipping and paid shipping because I am in a middle of developing parcel terminal shipping type using similar techniques which are in use in flat rate module.
Regards, K

kevster on July 18, 2013

Thx Ryan, very useful - I need to do this and also combine different rates for different products based on taxonomy field. I set your example up on a vanilla kickstart2 install on the standard shipping flat rate but it seems a bit hit and miss if I go back and fwds in the cart and also add extra products? (shipping 2.x)

It often seems to be lower than expected so if I increase the qty of an item from 2 to 3 it doesnt increase the shipping in the cart, maybe a rules weight issue?

Thx for all your commerce videos - watched lots now and so useful!

junetellain626 on January 4, 2014

That was really an easy to follow tutorial. Made me understand loops and rules better.

By the way, should I follow the same approach when doing flat rate shipping calculations with maximum amount of items?
Ex. $10 flat rate for "up to" 10 items.
Meaning if I buy 1 item, 2 items, or 10 items; shipping is $10.
If I buy 11 items, shipping should be doubled, $20... and so on.

I was thinking of getting the "total quantity in the order" and then dividing it by 10, and then multiplying the "ROUNDED UP" result to the shipping fee.
Ex. a. 10 items / 10 = 1, 1 x $10 = $10 shipping
b. 11 items / 10 = 1.1, 2 x $10 = $20 shipping

Unfortunately, I couldn't find a way to do this via rules. Tried to use execute php code to do the "rounding up" but one couldn't reuse variables inside this action for another action.

skenley on February 18, 2015

This works perfect as long as you don't have other shipping services enabled. I have created a "Free Shipping" service and am applying it to orders over $200. I have set up the condition on the Flat Rate shipping to be excluded on orders under $200. The problem I'm running into is that when the order is over $200, it still adds the calculation I have set up to add $0.50 per item. So, if I have an order with 10 items that totals $200, the free shipping costs $5. How can I exclude the calculation rule when the order totals over $200?