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

Closing #1596476: Help with shipping services rules as a duplicate of this one.

would love to know as well.

Anyone figured out how to do this in the meantime?

Status:Active» Fixed

You 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:

{ "rules_increment_standard_shipping" : {
    "LABEL" : "Increment standard shipping",
    "PLUGIN" : "rule",
    "REQUIRES" : [ "rules", "commerce_line_item" ],
    "USES VARIABLES" : {
      "list_item" : { "label" : "List item", "type" : "commerce_line_item" },
      "shipping_line_item" : { "label" : "Shipping line item", "type" : "commerce_line_item" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "list-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : { "input_1" : [ "list-item:quantity" ], "op" : "*", "input_2" : "400" },
          "PROVIDE" : { "result" : { "result" : "Calculation result" } }
        }
      },
      { "commerce_line_item_unit_price_add" : {
          "commerce_line_item" : [ "shipping-line-item" ],
          "amount" : [ "result" ],
          "component_name" : "flat_rate_standard_shipping",
          "round_mode" : "1"
        }
      }
    ]
  }
}

Then import this Rule:

{ "rules_standard_shipping" : {
    "LABEL" : "Standard shipping",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_shipping" ],
    "ON" : [ "commerce_shipping_calculate_rate" ],
    "IF" : [
      { "entity_has_field" : {
          "entity" : [ "commerce-line-item" ],
          "field" : "commerce_shipping_service"
        }
      },
      { "data_is" : {
          "data" : [ "commerce-line-item:commerce-shipping-service" ],
          "value" : "standard_shipping"
        }
      }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-line-item:order:commerce-line-items" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "component_rules_increment_standard_shipping" : {
                "list_item" : [ "list_item" ],
                "shipping_line_item" : [ "commerce-line-item" ]
              }
            }
          ]
        }
      }
    ]
  }
}

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

Title:flat rate calculation rule for two different servicesHow to define a flat rate service that gets incremented per product (i.e. $10 + $4 per product)

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.