How do I add shipping costs to orders? I have a really complex set of shipping rules

I have three product types

Product type A with a rate of £1 per product
Product type B with a rate of £2 per product
Product type C with a rate of £3 per product

A customer can add any combination of products into their cart so

2 x Product type A
1 x Product type B
1 x Product type C

I have created rules to loop through commerce_line_items and get the individual shipping costs for product types based upon a field in the commerce_product entity and I have got it so it calculates quantity times shipping cost.

What I do not know and cannot work out is how I then add these shipping costs back to the order, there seems to be no way to set the value of a shipping service.

Also I do not know how to parse the line items for each product type and calculate the shipping costs based upon each product type. So I need a condition check on the product type of each line item in the order to obtain the product type and then do the calculations based upon the correct shipping rate and quantity.

Any advice would be fully appreciated.
Edit the Reaction Rule "Collect rates: Flat Rate" and add conditions if you wish, so that it only applies to certain Product Types, or to an Order whose total is over a certain amount, for example. (Note that because you are using Conditional Rules you could also put such logic in the Action of your shipping pricing rule [see below])
Create a Shipping Service for your Flat Rate Shipping Method. For example, "My Basic Shipping." Give it a base rate - can be $0, or whatever.
Create a shipping pricing Rule that applies shipping charges to Products through the Service you just created. You can use conditionals in the Rule to apply the shipping based on SKU, Product Type, price, or whatever. You can use extra modules to base it on size and weight, day of the month, whatever.


Here's an example of a Price Calculation Rule. My Shipping Service is called 'swag shipping' and the Rule adds to the shipping cost based on the country:

{ "rules_ship_s_store_shipping_surcharge_by_country" : {
"LABEL" : "Ship\u0027s Store: Shipping Surcharge by Country",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "Commerce Checkout" ],
"REQUIRES" : [
  "rules",
  "commerce_order",
  "rules_conditional",
  "commerce_line_item",
  "commerce_shipping"
],
"ON" : { "commerce_shipping_calculate_rate" : [  ] },
"IF" : [
  { "data_is" : { "data" : [ "commerce-line-item:type" ], "value" : "shipping" } },
  { "data_is" : {
      "data" : [ "commerce-line-item:commerce-shipping-service" ],
      "value" : "swag_shipping"
    }
  },
  { "entity_has_field" : {
      "entity" : [ "commerce-line-item:order" ],
      "field" : "commerce_customer_shipping"
    }
  },
  { "NOT commerce_order_compare_address" : {
      "commerce_order" : [ "commerce-line-item:order" ],
      "address_field" : "commerce_customer_shipping|commerce_customer_address",
      "address_component" : "country",
      "value" : "US"
    }
  }
],
"DO" : [
  { "CONDITIONAL" : [
      {
        "IF" : { "commerce_order_compare_address" : {
            "commerce_order" : [ "commerce-line-item:order" ],
            "address_field" : "commerce_customer_shipping|commerce_customer_address",
            "address_component" : "country",
            "operator" : "is one of",
            "value" : "CA\u000D\u000AMX"
          }
        },
        "DO" : [
          { "commerce_line_item_unit_price_amount" : {
              "commerce_line_item" : [ "commerce_line_item" ],
              "amount" : "1000",
              "component_name" : "flat_rate_swag_shipping",
              "round_mode" : "1"
            }
          }
        ]
      },
      { "ELSE" : [
          { "commerce_line_item_unit_price_amount" : {
              "commerce_line_item" : [ "commerce_line_item" ],
              "amount" : "1500",
              "component_name" : "flat_rate_swag_shipping",
              "round_mode" : "1"
            }
          }
        ]
      }
    ]
  }
]


}
}

IMPORTANT NOTE: You may have trouble after you get your shipping working, to display your shipping line items in your Order display, for example in the Order acknowledgment e-mail that comes by default with Commerce. You need to make some rather complicated configuration changes to get this to work. Please see the article How to display all Drupal Commerce line items using a token if you get that far.