I use Drupal Commerce on Drupal 7 site, using product displays and products.
Each product has a shipping cost field.
At checkout, the shipping costs of all products in the order need to be collected and shown.
My approach is this: I use the Commerce Flat Rate module which creates the Flat Rate shipping method. I then create a flat rate shipping service ´Custom Shipping Cost´. Then I create a new shipping Rule ´Apply custom shipping costs´ which has a loop that needs to get the field value of the field ´custom shipping cost´ for each product in the order.
Question: how should a rule look like that gets the field values of a product connected to a line item and adds it to the shipping cost.
The following rules (which are nested) are not working. I figured out using Rules logs that in the third rule, Entity has field is not passed. I try to get the product field value which needs to be added to the total shipping rate.
0.528 ms Evaluating the action component_rules_get_shipping_price_field_value. [edit]
" Evaluating rule get local shipping price field value. [edit]
0 ms Evaluating rule get local shipping price field value.
0.049 ms Evaluating conditions of rule get local shipping price field value. [edit]
0.11 ms The condition entity_has_field evaluated to FALSE [edit]
0.113 ms AND evaluated to FALSE.
0.123 ms Finished evaluation of rule get local shipping price field value.
================================================
{ "rules_apply_custom_product_shipping_costs" : {
"LABEL" : "Apply custom product shipping costs",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "commerce_shipping" ],
"ON" : { "commerce_shipping_calculate_rate" : [] },
"DO" : [
{ "drupal_message" : { "message" : "Calculating shipping rates." } },
{ "variable_add" : {
"USING" : {
"type" : "commerce_price",
"value" : { "value" : { "amount" : 0, "currency_code" : "USD" } }
},
"PROVIDE" : { "variable_added" : { "custom_shipping_cost" : "Custom shipping cost" } }
}
},
{ "component_rules_get_local_shipping_rate" : {
"price" : [ "custom-shipping-cost" ],
"line_item" : [ "commerce-line-item" ]
}
},
{ "component_rules_get_international_shipping_rate" : {
"price" : [ "custom-shipping-cost" ],
"line_item" : [ "commerce-line-item" ]
}
}
]
}
}
==================
{ "rules_get_local_shipping_rate" : {
"LABEL" : "Set local shipping rate",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "commerce_line_item" ],
"USES VARIABLES" : {
"price" : { "label" : "price", "type" : "commerce_price" },
"line_item" : { "label" : "line item", "type" : "commerce_line_item" }
},
"IF" : [
{ "data_is" : { "data" : [ "line-item:type" ], "value" : "shipping" } },
{ "data_is" : {
"data" : [ "line-item:commerce-shipping-service" ],
"value" : "local_shipping_rate"
}
}
],
"DO" : [
{ "drupal_message" : { "message" : "Start loop: collect line item local shipping price field values" } },
{ "LOOP" : {
"USING" : { "list" : [ "line-item:order:commerce-line-items" ] },
"ITEM" : { "lineitem" : "lineitem" },
"DO" : [
{ "variable_add" : {
"USING" : {
"type" : "commerce_price",
"value" : { "value" : { "amount" : 0, "currency_code" : "USD" } }
},
"PROVIDE" : { "variable_added" : { "custom_shipping_cost" : "custom shipping cost" } }
}
},
{ "component_rules_get_shipping_price_field_value" : {
"custom_shipping_cost" : [ "custom_shipping_cost" ],
"line_item" : [ "line_item" ]
}
},
{ "commerce_line_item_unit_price_add" : {
"commerce_line_item" : [ "line-item" ],
"amount" : [ "price:amount" ],
"component_name" : "flat_rate_local_shipping_rate",
"round_mode" : "1"
}
},
{ "drupal_message" : { "message" : "Single product shipping cost is" } },
{ "drupal_message" : { "message" : [ "custom-shipping-cost:amount" ] } }
]
}
}
]
}
}
==============
{ "rules_get_shipping_price_field_value" : {
"LABEL" : "get local shipping price field value",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : {
"custom_shipping_cost" : { "label" : "custom shipping cost", "type" : "commerce_price" },
"line_item" : { "label" : "line item", "type" : "commerce_line_item" }
},
"IF" : [
{ "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } },
{ "entity_has_field" : {
"entity" : [ "line-item:commerce-product" ],
"field" : "field_shipping_rate_local_countr"
}
}
],
"DO" : [
{ "drupal_message" : { "message" : "line item has field commerce_product" } },
{ "data_set" : {
"data" : [ "custom-shipping-cost" ],
"value" : [ "line-item:commerce-product:field-shipping-rate-local-countr" ]
}
}
]
}
}
=========================== EDIT The following issue was causing my problem: In this whole rules setup i used multiple variables as line items. I mistakenly used a lineitem variable outside the loop, instead of the lineitem variable inside the loop. at a certain point i had lineitem and line-item and mixed them up which made it not work. Also, the product needed to be added as variable in the loop.