Discussions

Rules module - Access Commerce Option Sets

Hi there,

currently I have a commerce site with several products. Each product has several product fields like description, price etc. One field is of type "Option Set Reference". The option set consists of 2 fields:

1) text field
2) boolean field

My intention: Using "Rules" I'd like to create a rule that automatically increases the unit price by some amount. In that rule I'd like to check if user checked/unchecked the boolean field in the option set. According to the users input I'd like to increase the unit price or not:

a) if boolean field is TRUE -> increase unit price by amount
b) if boolean field is FALSE -> do nothing

I have following rule:

"rules_commerce_testing" : {
    "LABEL" : "Commerce - Testing",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_line_item", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "AND" : [
          { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } },
          { "entity_has_field" : {
              "entity" : [ "commerce-line-item:commerce-product" ],
              "field" : "field_sem_zertifikatsgebuehr"
            }
          },
          { "entity_has_field" : {
              "entity" : [ "commerce-line-item:commerce-product" ],
              "field" : "field_terminauswahl"
            }
          }
        ]
      }
    ],
    "DO" : [
      { "commerce_line_item_unit_price_multiply" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : ".8",
          "component_name" : "base_price",
          "round_mode" : "1"
        }
      }
    ]
  }
}

The 3rd condition is the relevant one. "field_terminauswahl" is the Option Set Reference. Using "Devel" I was able to get some evaluation log:

    * 0 ms Reacting on event Calculating the sell price of a product.
    * 6.663 ms Evaluating conditions of rule Commerce - Testing. [edit]
    * 7.954 ms The condition entity_has_field evaluated to TRUE [edit]
    * 8.337 ms The condition entity_has_field evaluated to TRUE [edit]
    * 8.469 ms The condition entity_has_field evaluated to FALSE [edit]
    * 8.481 ms AND evaluated to FALSE.
    * 8.492 ms AND evaluated to FALSE.
    * 8.601 ms Evaluating conditions of rule Calculate taxes: Sales tax. [edit]
    * 8.612 ms AND evaluated to TRUE.

As you see the 3rd condition evaluates to FALSE and I can't really explain myself why this is happening.

Is there something I'm missing? Please help me solve this issue.

Thanks in advance.

Cheers,

Victor

Posted: Mar 4, 2012

Comments

rfay Randy Fay on March 5, 2012

Well, it's saying that your commerce product does not have a field_terminauswahl field, as far as I can tell. If you have more than one product type, this may be a product type that does not include that field. It's possible that it's a rules bug, but normally it's a facepalm.

vdorneanu on March 6, 2012

This is what I did:

1) Add a Option Set reference to the product type "Produkt"
http://oi40.tinypic.com/6pajad.jpg

2) Add a Option Set
http://oi43.tinypic.com/zwge8x.jpg

3) Create new Product
http://oi43.tinypic.com/bhlt9l.jpg

4) Create rule

{ "rules_commerce_product_rule" : {
    "LABEL" : "Commerce - Product rule",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_line_item", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } },
      { "entity_has_field" : {
          "entity" : [ "commerce-line-item:commerce-product" ],
          "field" : "field_product_ref"
        }
      }
    ],
    "DO" : [
      { "commerce_line_item_unit_price_add" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : "1000",
          "component_name" : "base_price",
          "round_mode" : "1"
        }
      }
    ]
  }
}

4) Add Product to cart
http://oi42.tinypic.com/2l9i78i.jpg

5) Check rules log
http://oi39.tinypic.com/1q4r5e.jpg

So no "facepalm" thing.. :(

uweb on August 22, 2012

I am experiencing the same problem where the the condition for the entity having the option set field evaluates to false even though other conditions which uniquely identify the product type (ie, fields which only that particular product type has) evaluate to true. And, at the same time the specific option set is available in the data selector replacement patterns. Any hint? Looks like a bug to me.