9 replies [Last post]
sprugman's picture
Offline
Joined: 11/26/2007
Juice: 202
Was this information Helpful?

I think the title says it all.

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: What's the best way to restrict users to only order one prod

Hooking into the cart.

Here's one way I did it:

<?php
/**
* Implementation of hook_add_to_cart
*
* Disallows more than one product from being added to the cart from an Add To Cart button.
*/

function uc_test_add_to_cart($nid, $qty, $data) {
//drupal_set_message('<pre>'.print_r($data, TRUE).'</pre>');
//drupal_set_message('<pre>'.print_r($nid, TRUE).'</pre>');

$items = uc_cart_get_contents();

    foreach(

$items as $cartitem) {
      
//drupal_set_message('<pre>'.print_r($item, TRUE).'</pre>');
      
   
if ($cartitem->qty > 0 && $cartitem->nid==$nid) {
       
$result[] = array(
       
'success' => FALSE,
       
'message' => t('Sorry, you can only add 1x '.$cartitem->title.' to your cart at a time.'),
        );
    }   
    }
  return
$result;
}
?>

This prevents them from double-clicking an Add to Cart link and getting 2 items in the cart added. You can easily change this to restrict to just one item in the cart, period - just get rid of the $nid comparison.

--
Help directly fund development: Donate via PayPal!

sprugman's picture
Offline
Joined: 11/26/2007
Juice: 202
Re: Re: What's the best way to restrict users to only order one

Thanks. BTW, you might be interested in the devel module. It provides a handy function (dpm()) for this:

drupal_set_message('<pre>'.print_r($nid, TRUE).'</pre>');

It also has a corresponding dpr() which writes out a print_r statement without the drupal_set_message.

torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4110
Re: Re: Re: What's the best way to restrict users to only order

I've used the Devel module, and it's good for some things, but also breaks others (like some Ajax requests). That's why it's sometimes easier to just use drupal_set_message and then specify the object or array you're looking to dissect Smiling

--
Help directly fund development: Donate via PayPal!

sprugman's picture
Offline
Joined: 11/26/2007
Juice: 202
Re: Re: Re: Re: What's the best way to restrict users to only or

oh, I wasn't aware of that. I'll keep it in mind, thanks....

OnlineWD's picture
Offline
Joined: 04/16/2009
Juice: 132
Re: Re: Re: Re: Re: What's the best way to restrict users to onl

This works nicely thanks. I used this for some gift certificates with options for varying values. Works fine, don't know what other products might work like this but I cannot allow more than one item because the vouchers can be delivered to one address only and usually the delivery address is the recipient i.e. the person I might buy the vouchers for.

rachelp's picture
Offline
Joined: 07/16/2011
Juice: 5
where to put code?

noob q here.
where do i put this code
tried putting at end of uc_cart.module but still able to add more than one product to cart

royerd's picture
Offline
Joined: 01/22/2008
Juice: 293
Re: where to put code?

I've found this "limit qty" module works very well--just add the limit as a "feature" to the product.

Is this what you are looking for?

rachelp's picture
Offline
Joined: 07/16/2011
Juice: 5
want only one product of any kind

Thanks for the quick reply.
I'm trying to set up multi-seller environment for second hand goods, so there will only be one of any product ever.
Am using uc_multi-stock to prevent double adding to cart.
And uc_out of stock to prevent buying item that's been bought
However an item that's in a cart can be added by another user until order processed
Don't know and can't find way to prevent this so...
I'm thinking if they only have one product in cart ever and have to go to checkout and process order straight away I minimize risk of double buy by two different users
but can't get code "snippet" provided here to do anything, figure i've done it wrong - i have taken out the $nid comparison as suggested

DREAM SCENARIO - users can only add products from same seller into a shopping cart, must proceed to checkout and pay to buy of another seller
Is this at all possible??? - new to the forums - should i start a new question for this

jodo's picture
Offline
Joined: 01/10/2008
Juice: 28
oops

oops

My question was answered in the post. Saw after re-reading.