Discussions

How to dynamically calculate the sell price of a product using rules

I have a quesiton for the comunity!

I see that using:

Event: Calculating the sell price of a product Action: Set the unit price to a specific amount

Everithing works very good if i want just to change the price.

Now I'm trying to change the above "Event" with the purpose to use my event (to allow the data-chaining of my tables),
so i think that i have to put a
rules_invoke_event('my_event', $line_item, $new_price) into the same hook that call the " Calculating the sell price of a product " rules invoke event,
to ensure that it's really called during the module is calculating the price.

I tryed all this hooks:
hook_commerce_line_item_rebase_unit_price($line_item)
hook_commerce_product_calculate_sell_price_line_item_alter($line_item)
hook_commerce_price_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays).
hook_commerce_line_item_rebase_unit_price(&$price, $old_components, $line_item) <- it seems don't work! maybe it have a bug, maybe.

but whitout good result.

somethig better happen with
hook_commerce_cart_product_prepare
and
hook_commerce_cart_product_add

but I really don't want to modify codes into the commerce_cart_module, to avoid the problem of updating commerce_cart module in the future. I will like ti make my module independent.
(Using this way, i need to modify the commerce_cart module to ensure a way to chain my tables data during the data flow)

That's all! Somebody have any idea, or somebody can show me the right hook to catch and put inside my personal invoke_event to solve my problem?
Thanks for the support!

Stefano

Posted: Jan 20, 2012

Comments

stefano.marodin on January 20, 2012

I'm working with COMMERCE KICKSTART! is it possible that maybe I need a patch or something like that in the price or cart module to really render the price changes that my module do?? Somebody already do it with commerc e kickstart?
Thanks

stefano.marodin on January 20, 2012

Hi Rfay! thanks to answer me!! I really need an help1
i really already try everithing to solve this problem, i'm becoming crazy!

I will try to telling you what i do till now, tell me (if you can what do you think about, or better, the good way to solve it :D):

FIRST WAY (trying to do using your advice)(without my rules):

Event: commerce_product_calculate_sell_price
I tryed to put this

$line_item->commerce_unit_price['und'][0]['amount']=$new_price;
rules_invoke_event('commerce_product_calculate_sell_price', $line_item);

in all this hooks:

hook_commerce_line_item_rebase_unit_price <- i can't catch it!
hook_commerce_cart_product_prepare
hook_commerce_product_calculate_sell_price_line_item_alter
commerce_cart_line_item_refresh <- it only change the price in the cart block, not in the check-out (normal!)

and later in the action i put:

"Set the unit price to a specific amount"
Parameter: Line item: [commerce_line_item], Amount: [commerce-line-item:commerce-unit-price:amount]

But the price don't change when i add in the cart a product!

SECOND WAY TO TRY TO SOLVE IT:

I create a new event (i like it because i'm adding product in the cart that aren't in the commerce_product_table, they are product with diffent fields and customization. So, i need a way to ensure the data-chaining in the line_item data_selector when i set the rule action (and i think that the unique way to do it is create my event) with my parameters and entities. Later i try to put the "rules_invoke_event('my event')" in the same hooks that call the rules_invoke_event("commerce_product_calculate_sell_price")

and after the same action of above (with the parameter of my new_price in the amount of the new price)

I really think that what i'm doing is a right way! but the price never change:(

Just a little observation:
Is possible that the price of the product is something like locked or "disabled to change" from external modules?
I mean, maybe if I change the original module i can change the price quite easy.
Have i to put something in my module with the purpose of enable the price changing??

Thanks a lot! a really need an help, because it's some days yet that i'm trying to do this, and i don't have good results!

Stefano

rfay Randy Fay on January 20, 2012

I can't help you.

You would have to explain why you can't use a regular product pricing rule, which would be the way to solve this. You might have to add new rules conditions in your code, but should have to do nothing else.

stefano.marodin on January 23, 2012

ok. I tryed a lot to use my rules but they really can't change the product price. So, ok, i will use the regular product pricing rules. i will use (i think this is the way.. maybe not)
Event: commerce_product_calculate_sell_price
Action: Set the unit price to a specific amount

i need to give to the specific amount a value calculated in my "custom_product_table".
I have the ID of my table in the line_item "Data" field, so i can calculate this programatically.
i need to know what hook i have to catch to calculate my new price and how to give it back my changes later when the product is add to the cart.
maybe a code like that into a hook?
$line_item->commerce_unit_price['und'][0]['amount']=$new_price

and set the specific amount in the action pointing to $line_item:commerce_unit_price:amount
hoping that this will later change in the total amount cart block to!

maybe this way is right?
or if not, how do you do that or how can i do that?
please.. give me a more possible detailed way to do this using regular product pricing rules, because i really can't do it!

thanks Randy
Stefano

strae strae on April 23, 2013

I have the same problem.

My reason becose Rules doesnt work, is becose I need to have a different discount amount based on the product type...

Actually I put a textfield `global_discount` in the user profile, and in this I store the product type and the amount, in the form:

generic_product|10
shoes|8
hats|25

etc...

Now, I have few conditions to check when the discounts should by applyed, but my problem come in the action: in the amount data selector, I dont have the product; I tryed to use just commerce-line-item and then in the php evalutation retrieve all data i need (back the amount, user's global_discount and product_type) but:

1. Using commerce-line-item fail the amount type format.
2. Using commerce-line-item:line-item-id works (in the php evalutation I can then load all data I need) but ONLY for items already in the cart, for others the value is null.

Actually, Rules would fit my needs if would be possible either disable the amount data type or remove the amount data selectors (leaving the Line Item, and then dig to the amount via php evalutation)

BTW i dont understand why the prices are so stricly bounded to the rules, and looks impossible to alter prices without them.... is there any reason in particular?