Tags/topics: 
1
Answers
Vote up!
0
Vote down!

Per Product Shipping Buggy

I followed Randy Fay's video screencast (https://vimeo.com/32813567) to show how to add per-item shipping, however I am having and issue I can't seem to resolve. Here is an example (I have $3 per item setup):

- Add something to cart
- Go checkout (add the appropriate address info) and click Continue
- Goes to the Shipping page and shows the correct dollar amount for shipping and click Continue
- Finally on the Review order page and the shipping still looks OK, however if I click the Go Back link
- I got to the Shipping page and now it adds another $3.00 so one item is now $6. The think it's not being doubled, it's just another $3 being added so if I added 2 items it'll be $6 for shipping until I click the Go Back link then it'll be $9.00.

Here is an export of my Per-Item Shipping Calculation Rule (if that helps):

{ "rules_per_item_shipping_calcualtion" : {
"LABEL" : "Per Item Shipping Calcualtion",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules", "commerce_line_item", "commerce_shipping" ],
"ON" : [ "commerce_shipping_calculate_rate" ],
"DO" : [
{ "LOOP" : {
"USING" : { "list" : [ "commerce-line-item:order:commerce-line-items" ] },
"ITEM" : { "line_item" : "Line Item" },
"DO" : [
{ "data_calc" : {
"USING" : { "input_1" : [ "line-item:quantity" ], "op" : "*", "input_2" : "300" },
"PROVIDE" : { "result" : { "calculated_line_item_shipping" : "Calculated Line Item Shipping" } }
}
},
{ "commerce_line_item_unit_price_add" : {
"commerce_line_item" : [ "commerce_line_item" ],
"amount" : [ "calculated-line-item-shipping" ],
"component_name" : "flat_rate_per_item_shipping",
"round_mode" : "0"
}
}
]
}
}
]
}
}

Also here are the versions of Commerce modules I am using:

Drupal Commerce 7.x-1.3+51-dev (I just went to dev to see if it would help anything....)
Commerce Shipping 7.x-2.0-beta1
Commerce Flat Rate 7.x-1.x-dev
Drupal 7.15

I tried contacting Randy Fay directly via Twitter, however he said he's "very rusty on commerce stuff" and recommended for me to hit up #drupal-commerce. I haven't had much luck on IRC, so I trying here now, so I really appreciate any help.

Thanks,
Brent

Asked by: brentr
on October 2, 2012

1 Answer

Vote up!
0
Vote down!

hey brent,

I have approached this by adding a rules component which counts the number of shippable products in the order. Then your shipping method calculation rule can call this component using a calculation rule and your number of products is available for use in calculations.

This is the component:

{ "rules_calculate_qty_of_shippable_items" : {
    "LABEL" : "Calculate qty of shippable items",
    "PLUGIN" : "rule",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "line_item" : { "label" : "line item", "type" : "commerce_line_item" },
      "total_of_shippable_line_items" : {
        "label" : "total of shippable line items",
        "type" : "decimal",
        "parameter" : false
      }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } },
      { "entity_has_field" : {
          "entity" : [ "line-item:commerce-product" ],
          "field" : "field_commerce_shipable"
        }
      }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : { "input_1" : [ "line-item:quantity" ], "op" : "*", "input_2" : "1" },
          "PROVIDE" : { "result" : { "calc_total_of_shippable_items" : "calc total of shippable items" } }
        }
      },
      { "data_set" : {
          "data" : [ "total-of-shippable-line-items" ],
          "value" : [ "calc-total-of-shippable-items" ]
        }
      }
    ],
    "PROVIDES VARIABLES" : [ "total_of_shippable_line_items" ]
  }
}

You will need to remove the condition which is checking for the shippable checkbox, that is a custom field which I set up so that shippable and non shippable products can coexist on the same order and shipping will calculate correctly.

I then have a series of flat rates depending on the size of the product and add a calculation rule added which adds a per item amount to the base price of the flat rate.

Hope this helps

Bernie

Answer by: BernieCram
Posted: Oct 10, 2012