Drupal/Ubercart Check if a certain product is in the cart on checkout
Asked Answered
B

3

5

I have one certain product that needs to be in the cart under certain circumstances. I have been looking at the Ubercart api documentation and I don't see any hooks that would be the obvious place to see if a certain item exists prior to checkout.

I could use the hook_add_to_cart hook to add the special item whenever the first item is added, but I'm concerned that the visitor may remove the item and then complete the purchase without the required item.

Any suggestions how to make sure the special item is in the cart on checkout?

Babiche answered 23/9, 2010 at 19:50 Comment(0)
I
3

You can have a module and run something like:

function mymodule_init() {
   if (preg_match('/checkout/', request_uri()) {
      $items = uc_cart_get_contents();
      foreach ($items as $item) {
         // code
      }
   }
}

That will fire up on the checkout page, and fetch the cart contents. Everytime they hit the checkout page, uc_cart_get_contents() returns the cart contents.

http://www.ubercart.org/docs/api/uc_cart_get_contents

There are probably better ways to do what you want to do though, like using a Conditional Action to prevent checkout if Item B is in the cart but Item A is not. You can also look at Product Kits, but I don't have much experience with that.

Innes answered 23/9, 2010 at 20:40 Comment(0)
A
2

From what you have said it sounds as though the product kit module might be very much worth looking into as a way of ensuring any items associated with the main product are kept in the cart.

Product kit comes as part of ubercart and you will find it on the modules page under 'Ubercart - extra'. If this is no good then we can see about using the API :)

Asymmetry answered 23/9, 2010 at 20:8 Comment(1)
I don't think that the product kit module will work in my situation but thanks for the suggestion, that may be a place I can get some ideas how to implement a solution.Babiche
A
2

An old question, but I found a great solution.

hook_uc_cart_item_delete() functions specifically on certain entities when they are removed. You can just set this hook in your module, check for the specific entity being removed that is dependent on the other item, and then use uc_cart_remove_item() on the item you want to remove.

Allethrin answered 31/7, 2013 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.