Detecting where hook_cart_item is being called from

Posts: 13
Joined: 03/11/2008

Hi,

I am adding a stock check mechanism to do 3 things:

1. Remove "Add to cart" buttons if stock control is activated and set to zero stock

2. Throw an error is someone uses "Add to cart" on an item which has just gone to zero stock (i.e. someone just bought the last one)

3. Ensure that customers can't change the quantity in their basket to a value that exceed current stock

Point 3 is the one causing me a problem. It seems hook_cart_item gets called at least twice when you update a basket's contents as I get this when I try it with a product that doesn't have sufficient stock:

  • At present, we can supply only 4 of item MXR pédale de compression Super Comp M132
  • Votre panier à été mis à jour.
  • At present, we can supply only 4 of item MXR pédale de compression Super Comp M132

It would also be nice to be able to force the item quantity to the allowed level rather than rely on the customer to do it, which requires updating the basket contents at that point in the code too.

Am I asking the impossible??

Regards,

- Paul

P.S. I know I have a few more transalations to add!

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

I think for dealing w/ the cart view form you should look into hook_form_alter() instead of hook_cart_item(). With that you can check the values when they submit the form either for update or for checkout, check the stock, and throw a validation error if they've specified a value that's too high.