2

What I want: I want to give my products a weight and I want to calculate the total weight of the order.

What I did:

  • I'm using Drupal Commerce Kickstart 7.x-2.0.
  • I added a field 'weight' to the Product Variation 'Product'.
  • I created a custom Rule
  • Added a condition 'Entity is of type': commerce-line-item == commerce_product
  • Tried also to add or replace the condition above with the conditio 'Entity has field': commerce-line-item has field 'field_weight'
  • Create a Loop Action for commerce-line-items
  • Add a Loop Action but don't find the field 'field_weight'

Can someone explain me why I can't access my custom fields in Rules even though I added specific conditions?

Thanks in advance!

2
  • What's the cardinality of field_weight? I suspect if it's 1 that rules will be clever enough not to let you loop through it
    – Clive
    Jan 4, 2013 at 9:15
  • It's indeed 1 but I don't want to loop through field_weight, I want to loop all commerce_products. Then I want to add the Loop Action 'Calculate a value" and select field_weight.
    – Mr.B
    Jan 4, 2013 at 9:32

2 Answers 2

2

You're not looking for the field on the entity you've added it. You added it to the product, so in addition to your "Entity is of type" on the line item, then you need to check the type of the commerce-line-item:commerce-product for whatever product type you're expecting to find the weight field on. Only then will Rules know that that product has that field.

1

I know this was asked a few years ago so it's likely not relevant to Mr. B any more, however I had this same issue and spent a bit over a day figuring it out so maybe this can help someone else with the same problem.

As Ryan said the field_weight (custom_field) is part of commerce_product so another link is required. The path to a custom field is commerce-line-item:commerce-product:custom_field. However to get this field available within a loop, a rule component (which contains the conditions and calculations) must be called from within that loop. Also using "data comparison" rather than "has field" opens up all the fields within that entity.

To be able to reference these conditions you must have the module entity_reference installed. www.drupal.org/project/entityreference

Creating the rule component

  • From the rules page click on Components tab
  • Click Add new component
  • Component plugin = Rule
  • Add a custom name
  • Variables: Date type = Commerce Line Item, Label = Line Item, Machine name = commerce_line_item
  • Condition 1: Add new conditions -> Data comparision -> Data selector = commerce-line-item:type, Value = equals, Value = Product
  • Condition 2: Add new conditions -> Data comparision -> Data selector = commerce-line-item:commerce-product:type, Value = equals, Value = your_product_type
  • Action: Any action can now be used with the data selector commerce-line-item:commerce-product:custom_field
  • Processing or calculations that you would like done on each line item should be done here.

Example Rule Component

Creating the rule

  • Add new rule
  • Name = Your rule name, React on event = Completing the checkout process
  • Conditions are not required as these have been set up in the rule component
  • Add loop variable name = list_item
  • Add action within loop: Action to add = Components -> Rule: Your rule component name, Data selector = list_item
  • Any other actions that apply to the order as a whole can be added outside the loop

Example rule which loops through line items and calls rule component

Using this method any custom fields that you have created should now be accessible for all line items with a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.