0

I need to have different discount rates for every customer.

I added a textfield in the user profile, where I store a Json object with all the discounts (there is a little GUI to manipulate it), so, for example, I can have this situation:

{
    'product_B': 20,
    'product_A': 15,
    'product_C': 0
}

This means that this customer will have a fixed discount for all commerce_products of type A, 15 on type B and 0 on type C.

I got stuck in the Rules configuration: I created a rule for the Subtract an amount from the unit price event; In the condition, i just check that the user and the line item match few pre-requisites, and it works, but I cant get the line_item details in the action:

// My action
global $user;
$ul = user_load($user->uid);
if(isset($ul->field_discounts))
{
  $user_discounts = field_get_items('user', $ul, 'field_discounts');
  /*
   * Here i have to get few info of the current commerce_product/commerce_line_item
   * in order to calculate the correct $item_discount
   * */
  $discount = ((int)$item_discount * $value) / 100;
  return $discount;
}
return 0;

So the question is: inside that PHP block, how to get the current line item?

  • Have you tried $line_item? – Clive Sep 16 '13 at 13:10
  • yep, both $line_item and $commerce_line_item, always get null.. – Strae Sep 16 '13 at 13:13
  • @Clive and if i alter the data selector for that action, getting the line item instead of the amount, I get a error becose the value must be an amount – Strae Sep 16 '13 at 13:14
  • Hmm yeah, that makes sense thinking about it. I don't think the line item will be exposed to the PHP block, and I doubt there'll be anything in scope to get a reference back to it. You might need to create your own Rules action. It's pretty simple, and you can copy most of what you need from commerce_line_item_unit_price_subtract, which does have access to the line item entity – Clive Sep 16 '13 at 13:17
  • @Clive thanks, I never created a Rule's action before, i'll give a read at the documentation (p.s: your should be an answer, not a comment ;) – Strae Sep 16 '13 at 13:21
2

I don't think the line item will be exposed to the PHP block, and I doubt there'll be anything in scope to get a reference back to it.

You might need to create your own Rules action. It's pretty simple, and you can copy most of what you need from commerce_line_item_unit_price_subtract(), which does have access to the line item entity.

hook_rules_action_info() would be a good place to start.

2

HT: Clive for making the initial suggestion. If I were you, I would make an action that "provides" the amount to subtract in a Rules variable and then enter that as a parameter in the "Subtract an amount from the unit price" so you don't have to duplicate the logic that handles price components in your custom code.

Also, I wouldn't use the global $user, because an administrator may cause this order to be refreshed and it would be priced according to that administrator's discount field value. Instead you should be able to traverse from the line item to the order to the order owner. You should always use the order owner when constructing pricing logic, not the current (or global) user.

  • Hi, thanks for your answer. The hint to not use global $user is great, i'll do this way. Now i have a little question: there is a usable hook to alter the line item creation used for calculating the price? I like to add the uid and the info i need to compute the discount, instead of reloading the product and digging deep into the fields (i know they get cached, but is also more comfortable) – Strae Sep 17 '13 at 9:46

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.