Why can't I access my custom fields in Rules?
Update: The Discounts and Product Pricing Rules tutorial has a screencast with a complete demonstration of the techniques required here
When configuring Rules conditions and actions using the data selector, you may have encountered a scenario where you can't see any of the custom fields on entities passed as parameters for the rule's event. This is due to the fact that Rules only knows about the default fields that we tell it should function in a manner similar to entity properties. In order to access additional fields, you have to do one of two things in your conditions:
- Use the Entity has field condition to ensure the field you want to access exists on the variable you want to use it for, or
- Use the Data comparison condition to check the type of the entity (i.e. product type) to gain access to every field on the entity.
This example Rule can be imported into your site to demonstrate this aspect of Rules. It will apply a 10% discount to any product whose SKU is PROD-01:
{ "rules_discount_product_one" : {
"LABEL" : "Discount Product One",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules", "commerce_line_item", "commerce_product_reference" ],
"ON" : [ "commerce_product_calculate_sell_price" ],
"IF" : [
{ "data_is" : { "data" : [ "line-item:type" ], "value" : "product" } },
{ "data_is" : { "data" : [ "line-item:commerce-product:sku" ], "value" : "PROD-01" } }
],
"DO" : [
{ "commerce_line_item_unit_price_multiply" : { "line_item" : [ "line_item" ], "amount" : ".9" } }
]
}
}Unfortunately, there's also a bug in Rules that may account for the lack of fields in your data selector. In instances where you're looking for fields on an entity referenced by another entity (e.g. fields on the product reference by a line item in a product pricing rule), Rules will not acknowledge the existence of fields on the referenced entity. The workaround to this is to use Rules components to access field data on the referenced entities, but this method is really only feasible for advanced Rules users with a programming background. Your best bet will be to follow along in the issue waiting for it to be solved in Rules:
http://drupal.org/node/1053850
If you need to apply a workaround and don't have time to wait, you might glean some advice from this comment by EndEd in an issue pertaining to tax rules. Additionally, you could just write a custom condition / action or use a PHP condition / action through the Rules UI to access the field data directly.


Comments
Unique fields per Entity
This post is really important, especially for anyone trying to write rules to impact taxes, shipping, product prices, etc. What would be great to list in a table would be all of the unique fields each entity depends on (especially all of the line item types).
That way developers can understand how to properly scope the entities that they want to manipulate.
For those watching at home, I'm talking about:
In order for you to even target a commerce entity you must make sure the event is appropriate to the objects you want to manipulate.
Rule - Conditions Section
Add a condition of type "Entity has field"
target the "base" entity type that is in scope.
Select the field that is the unique or required for the entity. For example targeting line items where it contains the "commerce_product" field means the line item is a product entry, verses a line item that contains a "commerce_customer_address" as this would be a customer profile item.
I think it would be helpful to have this listed in one place for easy reference.
Keep trying with no success..
I am trying to do the follow:
I have a "website" content type which have a size vocabulary (big, medium..).
I am using the same terms as atribute in the product type field and created several products each with different term.
I create a rule that create a "website" for each lime item that have been purchase.
What I need to do is that the "website" that just created will get the term from the product(line item) that just purchase.
I have tried tons of way to get the line item term / product term in a rule action with no success. With "entity has field, data comparetion, set data value and much more...
Any idea how to accomplish this?
Thank you..