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

Why can't I access my custom fields in Rules?

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.

Ryan Szrama
Asked by: Ryan Szrama
on May 30, 2012

1 Answer

Vote up!
6
Vote down!

In order to access additional fields, you have to do one of two things in your conditions:

  1. 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
  2. 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" } }
    ]
  }
}
Answer by: Admin
Posted: May 30, 2012