uc_cart_links ignores add_to_cart hook overrides from other modules and still adds item to cart

Project: 
Ubercart
Category: 
bug report
Priority: 
normal
Status: 
closed

Hi Ryan -

I have implemented my own add_to_cart hook to prevent certain user roles from purchasing a product. However if the user accesses a cart link that takes them immediately to the checkout page (i.e. /cart/add/pID?destination=cart/checkout) my add_to_cart hook that returns a result with FALSE success gets ignored. Is this possibly just a module weight / load order issue?

I should note that to prevent this, I needed to duplicate my logic and implement a hook_cart_item function which immediately runs "uc_product_update_cart_item" to set the quantity to 0.

However from an API perspective it seems that it should be handled by the add to cart hook.

Re: uc_cart_links ignores add_to_cart hook overrides from other

Interesting - does this bug only occur if the Cart Link takes you to checkout?

Re: uc_cart_links ignores add_to_cart hook overrides from other

I'd need you to add some benchmarks to the code to see where the breakdown is. According to the code, the hook gets fired off and an item should only make it through to add to the cart if none of the hook funcs return FALSE. I do see in there a bit pertaining to redirects using drupal_goto(), but that still occurs before any items are added to the cart. So... I don't really know how this can be happening. Puzzled

Re: uc_cart_links ignores add_to_cart hook overrides from other

I'll look into it and see if this also occurs without going straight to checkout, I'll also try changing the module weights, but i didn't think that would be it.

I can confirm that the hook override works correctly when not using a cart_link api to add the item.

Ryan - any tips on where to look in the cart_links module for some debugging printing...

Sorry For wasting your guys time!

Hey guys -

I apologize, after further use testing exploring if it was only an issue on redirect to /cart/checkout, I can confirm that this is not a bug, and it's working Ok.

I think I was experiencing this earlier because I already had an item in my cart, so subsequent add_to_carts where still failing, but the cart was showing a qty of 1 on the same item from a previous add. This should not have been there, but I had another hook forcing a qty of 1 - which was happening there because the item was already in the cart.

Ok... getting to the root problem... (sort of)

Well after further testing and hook implementing - it seems my issue was related to how i was removing extra items from the cart in my hook_add_to_cart -

steps to reproduce: I add the same node to the cart on 2 separate calls, once with no product attributes, and once with attributes.

If I try to force a quantity of 1 by looping through my cart items to remove existing products using this call "uc_cart_remove_item($item->nid)" It does nothing, and no existing items are removed from the cart.

But if I force a quantity of 1 by using this method "uc_product_update_cart_item($item->nid, $data, 1)" and force it to a quantity of 1 - then it works.

I am pretty sure it is related to the two products not having matching attributes, but I am curious if I am trying to use "uc_cart_remove_item" incorrectly.

As a side note, I noticed while adding a product to the cart using a cart_link_api method, the "default" attributes of the product are ignored. It seems if default attributes are set on a product it might be useful to have the cart_link_api add the product with those attributes if no attributes are specified in the url to add the item to the cart.

Hope this helps anyone who comes across this post, and may help prevent future use case bugs.

Thanks Ryan, and torgos-

Re: Ok... getting to the root problem... (sort of)

Regarding your first issue, it removing/matching items in a cart does indeed depend on the attributes and more... you must match nid and a serialized data array or it won't work. Also, are you trying to force only 1 item in the cart at all or 1 item of each type? You can always use uc_cart_empty() to clean it out totally before adding the new item.

Regarding the cart link API and default attributes... I can see that being a possibility, but for now it's a little out of the scope of the module. It's meant to be pretty lightweight, and admins can already find the default product string to use by making sure the debug messages are on and adding a product to the cart.