0

I want to give a specific user a price override on specific products. In doing so, I added field collection on the people account setting. This field collection has to two fields: product sku and price.

I also created a rule with this codes:

{ "rules_price_override" : {
"LABEL" : "price override",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "commerce_line_item", "commerce_product_reference" ],
"ON" : { "commerce_product_calculate_sell_price" : [] },
"IF" : [
  { "user_has_role" : {
      "account" : [ "site:current-user" ],
      "roles" : { "value" : { "2" : "2" } }
    }
  },
  { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } },
  { "entity_has_field" : {
      "entity" : [ "site:current-user:field-price-override:0" ],
      "field" : "field_product_sku"
    }
  },
  { "data_is" : {
      "data" : [ "site:current-user:field-price-override:0:field-product-sku" ],
      "value" : [ "commerce-line-item:commerce-product:sku" ]
    }
  }
],
"DO" : [
  { "commerce_line_item_unit_price_amount" : {
      "commerce_line_item" : [ "commerce_line_item" ],
      "amount" : [ "site:current-user:field-price-override:0:field-regular-price:amount" ],
      "component_name" : "base_price",
      "round_mode" : "1"
    }
  }
]
}
}

But it only good for the first field collection value.

What will be the best way to make it loop, so that it can change its index as site:current-user:field-price-override:0 - end

0

I'm about sure that the machine name of your field collection field is site:current-user:field-price-override (your question doesn't state that explicitly), for which you do not have a "Entity has field" Rules Condition yet (you have site:current-user:field-price-override:0 instead).

Try adding a Rules Condition "Entity has field" (prior to being able to use it anywhere later on in your rule), which refers to your field collection field. Not sure why you have/need that site:current-user:field-price-override:0 Rules Condition, but you might not need that anymore.

You can then add a Rules Action which is simply a Rules "loop", and which iterates over each of the field collection items. And you will be able to use any of the typical tokens (in the replacement patterns).

For way more details about this, refer to "How to iterate over all field collection items in an eMail body using Rules?" (which also includes a rule in export format you may want to experiment with, if you only adapt some machine names of the used fields).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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