Discussions

Complex price calculation using a field in the product

Hi everybody

I'm trying to do some complex dynamic price calculation using rules.

The event used is the usual "Calculating the sell price of a product", no conditions and the action is "Add an amount to the unit price". So far nothing fancy.

The calculation uses a field in user's profile and another one in the product. Due to complexity, I need to use PHP so in the "amount" field, I select "commerce-line-item:commerce-unit-price:amount" as data selector and PHP evaluation.

I can access the field in the user profile using this code:

<?php
global $user;
$usuario = user_load($user->uid);
return (
$usuario->field_cantidad['und'][0]['value']);
?>

But I really have no idea how to access the product I'm trying to calculate the price.

Any help is welcome.

Posted: Sep 21, 2011

Comments

rfay Randy Fay on September 21, 2011

Take a look at other product pricing rules, like the ones in Commerce Tax (a submodule). You'll see that they get a line item, and the line item may be where you want to get your data from. If not, the line item has a product reference field on it.

perrymanson on September 21, 2011

Hi Rfay, I've been looking the rules you said and they don't use PHP inside the rule so it's not what I'm looking for.

My question is how can I access the field data of the product inside the rule using PHP.

I don't know if it's possible, though.

rfay Randy Fay on September 21, 2011

It's probably very poor form to use PHP directly in a rule; Far better to define the action in a small module. Even having the PHP filter turned on on a site is considered a major security issue. So yes, those rules do have PHP. They're in the module!