Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 496
Icode4food Avatar asked Sep 23 '10 19:09

Icode4food


2 Answers

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.

like image 86
Kevin Avatar answered Oct 20 '22 01:10

Kevin


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 :)

like image 38
hookd Avatar answered Oct 20 '22 01:10

hookd