Altering product node price dynamically

Posts: 11
Joined: 09/11/2008

PREVIEW
How can I alter the price in a node before placing in the cart? Is there something like cart_items that I can hook into in the regular node view of the product?

STORY
I have been hacking uc_coupon so that it works a bit like uc_affiliate in that I can use /coupon/c001/node/6 and store the coupon cid in the session so I can keep a track of it right to the end.

Currently if I add a product to the cart I can alter the price in the cart with the hook_cart_items and use that. This really helps when combining coupon usage and affiliate commissions, because commissions should be calculated on the discounted price not the regular price so having the price already discounted (not just a line item) works well. But I digress.

What I want to do is to change the price before they add the product to their cart. People might get confused if they use a coupon embedded link but still see the regular price before adding.

Is there a hook or a hack I can employ to make this happen? I have looked through the hooks I know about with UberCart but think I might need to look further afield for this.

So far this is just off the top of my head, I am thinking I will need to use a callback on /arg(0)/arg(1) where arg(0)=='node' and load node from arg(1) then if (in_array($node->type, array_keys(uc_product_node_info()))) I can use hook_node_info perhaps

Any thoughts out there?

Cheers,
Stuart

Posts: 11
Joined: 09/11/2008

Sorry to bump this, but does anyone have any ideas on how to alter the price in the node view before adding to cart.

hook_cart_items works great after it is in the cart, but I need to do this before adding.

Stuart

Posts: 1300
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

I'm not sure you can manipulate the sell price temporarily without editing the $node. (The sell price is located in $node->sell_price) ... However I imagine something could be done by creating some kind of array or other object that would follow either the user or the session, perhaps an array/value pair that tracks the product's nid (or SKU) and the discounted price. That way when you parse the final cost during checkout, your custom module can grab those array values and compare the SKUs or nids in cart to what is in the array. Anything that matches could generate a dynamic line-item, sort of like an "on-the-fly" discount for each item based on their nid or SKU.

Kind of an abstract idea, but that might be how I'd attack it.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 11
Joined: 09/11/2008

I am pretty sure I have to alter the node itself too. By looking in uc_product.module and the function uc_product_view I can see where most of that is built initially, I need a way to alter/override that.

Perhaps a form hook or something. So I give someone the coupon http://mydomain.com/coupon/coup011/node/5 and the coupon gets added to the session and they view the node. I would like that node to reflect the price decrease (at least the text if not the real price) because I can do the proper price alteration in the cart_items as soon as it's added.

Cheers,
Stuart

Posts: 1300
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

The problem with editing the node, is that it's not a user-context scenario. If you edit the node for one person, it changes for everyone. Which means if Customer A has a different discount option than Customer B, but Customer A puts in the discount first, Customer B might see the price change during his own checkout process, or when viewing the node.

Although I think you can edit the display price, and all that would entail is editing your product template. Your sell_price field could take the current viewing user's discount into consideration and perform math on it. You're not really editing the node's price at this point, you are simply manipulating the display. Does that make sense to what you need?

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 11
Joined: 09/11/2008

Yeah that makes sense, I realise that when I said alter the node it wasn't the node as a whole but a user's view of the form for that node.

I just don't know the best way and place to get in and alter it.

UPDATE

Been playing around with hook_nodeapi and that gives me exactly what I need, grab the $node->content['sell_price'] array and swap out the #value with my own.

Nearly done.

Stuart.

Posts: 11
Joined: 09/11/2008

Works like a charm,

thanks for your help.

Right from the start I thought hook_nodeapi was where I had to play but thought it would be far more difficult than it was. It was nearly as easy as using hook_cart_items.

Stu

Posts: 14
Joined: 11/04/2008

This looks like the solution I need! But I have NO idea how to do it. PLEASE HELP!

Here is what I need:

  • I have a group of products with varying prices, denominated in Euro's
  • I want to let the user select which currency they would like to use (say Dollars). All product display prices in the catalog and product list should then show the up-to-date converted, formatted amount (in Dollars)
  • When the user adds the product to the cart, I need the price to be added as the display amount above, not the Euro price
  • Since exchange rates change every day, I want the price of a user's cart items to stay the same once it has been added, but the display price in the catalog must update each day
  • When the user adds the new item to their cart, I want to ammend a description field, which appends what the exchange rated and time used were

I am currently using the currency_api to do the conversions, and have used a computed field in the product content type to do the calculation.

QUESTIONS:

  • How do I set the display price equal to the computed price field?
  • And how do I ensure that when the item is added to the cart, that the display price at the time is used as the product price?
  • How do I append a description field (or even the title) of the item added to the cart, to include additional buy-time text info?

Please help me, as this has frustrated me for weeks!