Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
I am trying to set up 2 flat rate shipping services each of them with a different base price + different cost per item. (Say service1: $10 base rate + $4 per item. service2: $15 + $6 per item).
I cannot use the same calculation rule for both services.
Any ideas how to address this?
Thanks very much.
Cheers.
Comments
Comment #1
Simon Georges CreditAttribution: Simon Georges commentedClosing #1598076: Different shipping services with one calculation rule? as a duplicate of this one.
Comment #2
Simon Georges CreditAttribution: Simon Georges commentedClosing #1596476: Help with shipping services rules as a duplicate of this one.
Comment #3
NickWebman CreditAttribution: NickWebman commentedwould love to know as well.
Comment #4
johnnynia CreditAttribution: johnnynia commentedAnyone figured out how to do this in the meantime?
Comment #5
rszrama CreditAttribution: rszrama commentedYou would simply create two flat rate shipping services and then add two rate calculation rules to handle the additions. The base price would be $10 / $15 respectively, and what you'd want is a condition that checked the value of the commerce-line-item:commerce-shipping-service field to make sure you were incrementing the price the appropriate amount based on the service being calculated for. (Make sure you use "Entity has field" on the commerce-line-item to ensure the commerce-shipping-service field is there first.)
Now, once you make sure you have the right shipping line item type for the increment value (i.e. $4 vs. $6), you're going to have to use Rules' "Calculate a data value" action to prepare the amount you're going to increment the unit price amount by. Remember that prices are calculated in cents, so what you'd want to do is multiply the total quantity of all products by 400 or 600 respectively. The challenge is getting ahold of the total product quantity - there's a condition that can check it, but there is no simple token that you can use to calculate it.
What I would do is loop over the commerce-line-item:order:commerce-line-items, check to make sure it's a product line item, and then increment the shipping amount that number * the quantity of each line item. Unfortunately, because this means we need a condition in the actions section (where the loop is), we need to pass each line item in the list to a Rules component to perform the actual calculation.
Here's an exported example that you can see; you just first need to create a flat rate service called "Standard shipping" with a machine-name of standard_shipping to make sure the tokens all line up. Let's assume it's the $10 + $4 / per. example.
Import this Rules component first:
Then import this Rule:
One caveat - this works as advertised, but I'm seeing a Rules warning that looks to me like a bug in Rules itself. I'm running Rules 7.x-2.2, and the code for the notice looks pretty innocuous to me, so I posted a patch here that folks can use until Rules is fixed: #1843174-1: Calculating a data value results in an illegal offset warning
Comment #6
rszrama CreditAttribution: rszrama commentedComment #8
mrP CreditAttribution: mrP commentedIs this still valid for commerce_shipping 2.x? I tried running through this example and couldn't get it working.
Comment #9
mrP CreditAttribution: mrP commentedNever mind, just ran across Randy's video on the topic -- Using Calculation Rules to Add Per-Item Shipping in Commerce Shipping