update_cart_item hook / quantity question

Posts: 61
Joined: 01/02/2008
Bug Finder

I've been playing around with creating my own contribs, i want to be able to validate / control when users update their cart (changing quantity), but update_cart_item seems like a strange one. I was hoping for somthing more like add_to_cart.

For a start its in product.module not cart.module as you might expect?

Also it seems to not be a hookable function as its called with module_invoke not module_invoke_all.

All that said, I'm new to this level of module development so may have the wrong end of the stick - or wrong stick.

Why i care :

I have a product (ticket) which has a attribute (email), buying 3 tickets with different email addresses is expected, but NOT 3 tickets with the same email address.

Ideas?

Posts: 4116
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Hmm... regarding the hook, you'll have to chat with Lyle about it. I'm not really sure of all the details. For your application, it seems like you may be needing to limit unique items in the cart? It seems like you'll want to not show the quantity box on that page and hook into the add to cart form to make sure an item w/ that e-mail doesn't already exist in the cart and then also hook into the cart update form to force the quantity to stay at 1. I think in the future we really need a way for the cart form to control whether or not to display a quantity box. For now you may just have to alter the form to force it to be 1 and make that a bit of text instead of a form textfield.

Posts: 1968
Joined: 08/07/2007
AdministratoreLiTe!

A hook_add_to_cart() exists, but uc_product doesn't implement it. It doesn't need to do anything extra when a product is added to the cart. I think the only module in core that does use it is uc_product_kit, though it's doing some very strange things there.

You should be able to use this hook to compare the attributes of all the things in the cart with the product currently being added.

Posts: 61
Joined: 01/02/2008
Bug Finder

I saw hook_add_to_cart() which works great (used it on my first contrib http://www.ubercart.org/contrib/3477), but its not called when you update the quantity of an item on the cart page, only when you add a product, which i sort of understand as your not technically adding a product, just updating it.

Altering the form to make quantity text instead of a form field, does sound like the best solution, or i may just be lazy and use CSS to hide the column.

Still it might be an area that needs looking at - perhaps changing quantity does need use hook_add_to_cart (avoids needing 2 hooks for quantity checking). Also may be a bug on my site, but it doesn't look like the model details are being passed in the $data var on hook_add_to_cart();