Im trying to figure out how-to or if its possible to create a rules that use a different price (i have already a different price field inside the product entity) on teasers or full nodes depending of the user role. Im looking for a tip to get me started with rules in this scenario.

At the moment I´m stuck with:

Create a new rule.
Event->User has logged in.
Condition->User has roles (check the role i´m interested)
Action-> Here i dont see any action that fit. I see some calc actions relate to line items.

I suppose along the way I will need my custom field price so it will be necessary to use components to access it ?¿?

Comments

rszrama’s picture

If I understand your scenario right, you're just needing to create a "Product pricing rule" that discounts the price based on whether or not a user has a particular role. You can import this for an example (doubles product price for administrator role):

{ "rules_double_product_prices" : {
    "LABEL" : "Double product prices",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_line_item", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "3" : "3" } }
        }
      }
    ],
    "DO" : [
      { "commerce_line_item_unit_price_multiply" : { "line_item" : [ "line_item" ], "amount" : "2" } }
    ]
  }
}

Does that get you far enough?

EndEd’s picture

well thanks for that but what im really trying to do is to mimic the same behaviour you get with the Ubercart Price Per Role module (http://drupal.org/project/uc_price_per_role)

I´ll try to do my best to explain it:

So in the product entity i made a custom price field (apart from the default one 'commerce_price'). Lets call it 'field_product_role_price'. I fill both prices with Feeds so then every product entity have 2 prices. I create the product displays and by default, the price I see when i check the teaser or fullnode is the one that cames from commerce_price product entity field.

I want (if posible) to use rules to change the price you see on product display nodes by role. So change 'commerce_price' for 'field_product_role_price' as the default price at the display level.

If im Role1 the price i see in a node comes from 'commerce_price'
If im Role2 the price i see in a node comes from 'field_product_role_price' (my custom price)

Also this have to affect at the line-item level so the order get the right price.

Your solution is great to do calculations based on the commerce_price product entity field at the line-item level but I my case i get the role price via feeds with different pre-calculations per product so there is no pattern (double the price)

If rules alone can't handle it i'll try to make a module. Any advice about how you would do it will be great.

rszrama’s picture

It's the same thing, just instead of using the "Multiply unit price by some amount" action, use the "Set the unit price to a specific amount" action using the data selector for your custom price field. The only problem is that w/ the Rules bug for accessing fields of referenced entities, I'm not sure if you'll be able to select your custom price field or not. You might have to come up with a custom action until that's fixed in Rules.

EndEd’s picture

Well, thanks for the tips and the patience. I finally done it using php evaluation.

{ "rules_double_product_prices" : {
    "LABEL" : "Change Price per Role",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "php", "commerce_line_item", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "4" : "4" } }
        }
      },
      { "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "commerce_line_item_unit_price_amount" : {
          "line_item" : [ "line_item" ],
          "amount" : {
            "select" : "line-item:commerce-product:product-id",
            "php" : { "code" : "$product = commerce_product_load($value);\r\nreturn $product-\u003efield_product_tapper_price['und'][0]['amount'];" }
          }
        }
      }
    ]
  }
}
pcambra’s picture

Status: Active » Fixed

Probably you'd better create your own custom action for this instead of having raw php there because it could cost you some security extra risks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

hozt’s picture

Status: Closed (fixed) » Active

Thanks for the code EndEnd, it works well as an interim solution until the rules module is fixed.

@pcambra do you have some insight how to do this as a custom action?

Here is the rules bug:
#1053850: Fix metadata-assertions for referenced entities.

rszrama’s picture

Status: Active » Closed (fixed)

I'm going to reclose this issue, as the original post has been solved. The Commerce queue isn't really for general Rules discussions, though you can feel free to find help in the IRC channel or in a support request in the Rules tracker. I'd really just suggest looking at code that's similar to what you're trying to do and adapting it based on other Rules action definitions.

westie’s picture

I want to do something very similar but cant find out how to use the PHP evaluation in rules to pull a value from a product field...

Where are you able to find "$product-\u003efield_product_tapper_price['und'][0]['amount'];" from?

$product-\u003e <- never see this before

field_wholesale_price <- only bit I know

['und'][0]['amount'];
pitxels’s picture

@rszrama
hi, having #1053850: Fix metadata-assertions for referenced entities. fixed , could you please point how Rules can be used for having a price per role scenario. I know this is not a Rules discussion, but comming from using ubercart and not knowing what is an entity, I am very noob at this concept. thanks

rszrama’s picture

I'd check the forums on DC.org for existing discussions first.

pitxels’s picture

Price discounts were indeed documented, but not price per role.

I am posting here what I finally think is a clue (not tested yet)
I hope this link is more helpful that asking users to search, if not the case I apologize.

http://www.drupalcommerce.org/node/981
and http://www.drupal.ru/node/66535 (russian)