Discussions

Which is the best way to Alter Product Pricing in a complex scenario?

Hi,

I'm a Drupal Commerce newbie (almost a Drupal newbie too). I'm testing Drupal Commerce to see if i could use it for my e-commerce projects instead of another solutions like Magento, osCommerce, and so on. I've installed Commerce Kickstart and I've been testing it, i've been reading the Drupal Commerce documentation, etc. Drupal Commerce seems pretty nice, but I need anybody who points me in the right direction so i can customize it for my scenario.

This is my scenario:

I'll have N types of customers, being N and integer (2,3...).
I'll have a user field who will identify the type of customer for each user.
The price of a product needs to be different for each type of customer.

So, I need to have N different prices for each product. When a customer of type X is browsing the commerce site, it must see the prices for the X type of customer and those prices must be the prices used for that type of customer along all the shopping process (displaying prices, viewing prices in the shopping cart, prices in the order, etc).

I've been trying to do that for a scenario with N=2. In this secenario, for the first type of customer i want to show one price and for the second type of customer i want to show a different price, so:

I've created a second price field of type "Price" for the "Product" content type.
I've added a new field to the user entity for the information about the type of customer.
Then, I've tried to change the product prices using a "Product Pricing Rule" reacting to the "Calculating the sell price of a product" event.
I've added a "Entity has field" condition to load the current user and the type of customer field in the context.
Then, I've added another condition of type "Data comparison" to compare the type of customer field with the second type of customer.
Then, I've added an action of type "Set the unit price to a specific amount".

If i set manually the amount it works fine. It is useless, but it lets me know that the type of customer condition is working. I've tried to change the action to change the price for the value in the second price field i've created for the "Product" content type but i couldn't achieve it. I couldn't select the second price field to set the unit price value.

After having tried many things, i've stopped trying this way and I started to try new ways to achieve the desired results. So, I've been reading the documentation about the Drupal Commerce code, reviewing Drupal Commerce's code and so on.

So, I've created a new module which implements the "hook_commerce_product_calculate_sell_price_line_item_alter". This way i can change the price of the products showed in the site, but when i add a product to the cart, the price showed in the cart is not the price i've seen prior to click on the "Add to cart" button.

I've tried a few other hooks: "hook_commerce_cart_product_prepare", "hook_commerce_cart_product_add", "hook_commerce_price_field_formatter_prepare_view" and "hook_commerce_price_formatted_components_alter" but i couldn't achieve the desired results.

So these are my questions about Drupal Commerce:

1.- Is there a way to do what i need using a "Product Pricing Rule"?.
2.- Which is the way to do it programatically? Which are the involved hooks i'd need to implement?.
3.- Whis is the recommended way to achieve the desired results? Using "Product Pricing Rules" or implementing hooks?.

And that's all for the moment. Many thanks in advance, i'll keep trying while waiting for responses from the Drupal Commerce gurus...

Posted: Dec 15, 2011

Comments

stefano.marodin on January 11, 2012

Hi! i have the same problem of you! i need to add products to the cart with differrent prices depending on the state of the product. Do you solve it? how did you do? It's about 2 days that i'm trying to do it.
I tryed in the views.inc modifying the original button add-to-cart-form

function my_module_views_data_alter(&$data) {
$data['my_module_op']['entry_my_form'] = array(
'field' => array(
'title' => t('Entry my form'),
'help' => t('Display my form for the product.'),
'handler' => 'my_module_handler_field_change_price_form',
),
);

I create an handler named "my_module_handler_field_change_price_form" that is the same of the original commerce_cart_handler_field_add_to_cart_form.inc that manage the add-to.cart button.
I put inside my stuff, trying to change the line_item and trying to catch and change the price of the current product catched. But the price still be the same in the add-to-cart.

If you wanna try this way, maybe you can be find a solution just copying and modifying the original add-to-cart button, but if you already solve the problem, please, tell me the right hook or the right way!
Thanks a lot!
Stefano

GoddamnNoise on January 11, 2012

Hi Stefano,

Sorry, but I haven't found a solution for this problem yet. There is no much good information about Drupal Commerce yet and I haven't received any help in the forums. I'll try your approach too and I'll post here a solution if i can find it. Thanks a lot for your help. Please, post here a solution if you find it before i find one.

Sean on January 11, 2012

I did this on my site a while back using Prince Manfred's "Commerce Extra Rules" module. It provides more rules for Commerce and you can find it on D.o.

Post back after you try it out if you still need help but it's pretty straight forward.

stefano.marodin on January 12, 2012

Hi Sean,
I'm reading the extra rules condition module features. It really don't seem the good way to help me. My probelm is that i need to change the price of the product in the cart depending of different values inserted into another table. So i need to pass values in the cart to now what product i'm talking about and depending of it, make a query into this values table and decrease the product price with the result of this last query.
So i need to do it programatically. Try to see this page:
http://digibits.org/blog/drupal-commerce-dynamic-line-item-titles
with this, i can change the name of the product in the cart, but not the price.
I need something like that, or a hook to catch that can allow me the choise to change the price in the cart.
And also, i way to pass nid and pid.
Thanks if you can help me i really apreciate, because i really try everithing during this days, but whitout success.
Stefano

stefano.marodin on January 12, 2012

Hi, i forget to ask a thing. Somebody now why I'm not able to catch this hook??

function my_module_commerce_line_item_rebase_unit_price(&$price, $old_components, $line_item) {
//recalculate the price
}

I think that with this hook i can solve the problem, but it seems not working:(

GoddamnNoise:
I'm really thinking about install http://drupal.org/project/commerce_custom_line_items and make my own line item type. have you already tryed with this?

Qubical on January 15, 2012

Hi GoddamnNoise,

I have had a similar issue although slightly simpler in a project I'm working on.
My client wanted to have a coupon code that would allow the price of a certain product set to zero.

We used the rules to "Calculating the sell price of a product" and multiplied the amount by 0 based on the code and the product we attached to the code. The code was saved in the session.

If you write a module that implements it's own rules you should be able to use the event "Calculating the sell price of a product", then as a condition check the type of customer (N) and if the product has a price entered for the customer type.
Apply the adjusted price from the product, set it in the action part of the rule.

This would mean you would have to specify the prices in the product itself for each customer type.
If no special price was set it should just use the default price.

GoddamnNoise on January 15, 2012

Hi Qubical,

Thanks a lot for your answer, that really seems the best option to solve the problem. I've never code my own rules before, but i'll definitively try this way, it sounds pretty good. Thanks again for your help.

stefano.marodin on January 19, 2012

Hi Qubical! I'm also trying to change the price of a product before putting it in the cart and i made my own event, and I have a question for you:

I need to use this actions:
Commerce line item- set the unit price to a specific amount
Commerce line item- substract an amount to the unit price

But its don't works. I mean, Everytime i use this actions, using my event but also with original events,
when the definition action page (during the creation of the action) ask me to put something into
LINE ITEM (the first-one)
Data selector:
and i put:
commerce-line-item:commerce-unit-price:

the validation give me back this error:
Data selector commerce-line-item:commerce-unit-price: for parameter commerce_line_item is invalid.

Why? i really don't understand. It's an original event! do you have any idea to solve it?
Thanks a lot!
Stefano

Qubical on February 18, 2012

It depends on when you are fetching the price, does DC know which line item you are trying to update?
I think you should be looking for the cart content to expose the line items inside. Then update the price of the line item there.

If you want to modify the price shown to the visitors you'll need to look at "calculate the selling price of a product" which is called whenever DC needs to show a price.