Drupal 6 Ubercart: restricting quantities

8th Jun 2010 // By Chris // Drupal Planet

Recently we created a Drupal 6 store using Ubercart 2, but we just wanted users to be able to purchase one item at a time. This posed an interesting, but not insurmountable, problem.

The main issue here is that by default, the user may visit his or her shopping cart and choose any quantity of items. In order to restrict the user to force him or her to just buy one, we had a few options:

  • Hide the shopping cart but this is not very robust and an advanced user could easily get to it again. Also, because a message such as 'X has been added to your shopping cart' appears at the top of the user's screen, and the 'shopping cart' part is a link to the cart, this wouldn't work.
  • Use the uc_restrict_qty module to restrict the quantities. The uc_restrict_qty module is great and offers a simple menu-driven approach to restricting quantities, but this would rely on the site's administrator remembering to restrict quantities on each product in the store. We wanted all products to be restricted to a single item, so this seemed prone to human error.
  • Disable the quantity field on the checkout pane but although this seemed like a sensible option, it turned out that overriding Ubercart's default display of the checkout was not easy, as it did not seem to abide by the usual Drupal theme functions. We didn't want to hack Ubercart core, because this would hinder future upgrades, so we sought a simpler solution.
  • hook_form_alter the form responsible for the shopping cart which is uc_cart_view_form, and alter the #theme callback for all the quantity fields. By setting this to check_plain($element['#value']) it is possible to prevent the user from changing the value at all.

In the end, we utilised an Ubercart hook called hook_cart_item() which fires each time the cart is loaded. It checks the quantities of each item in the cart, and sets the quantity to 1 if it's not already 1. It's not the most elegant solution, but it does the job. Our implementation of the hook looked something like this:

  1. /**
  2.  * Implementation of ubercart's hook_cart_item.
  3.  */
  4. function mymodule_cart_item($op, &$item) {
  5.   // Restrict all quantities to 1.
  6.   if ($op == 'load' && $item->qty > 1) {
  7.     $item->qty = 1;
  8.   }
  9. }

It should be noted that this does not cater to the needs of the users, who will need to know that the shop imposes a 1-item limit. This will simply reset the quantity to 1 even if the user chooses more items, so unless there is a message to this effect somewhere on the site, the user might conclude that the site is not working properly.

We chose to implement a custom checkout pane with a special message to give the users some guidance, although perhaps that's a topic for an entirely separate article!

About The Author

Chris's Profile Picture
Chris

Chris Cohen is a seasoned web developer who began in Perl, before moving on to Java, C# and currently PHP and Drupal. He regularly attends DrupalCon and is "Certified to Rock" (certifiedtorock.com).

7

Comments

fenstrat's picture

You can form alter the uc_cart_view_form form and change the #theme callback for the qty field for each (or any particular) item in the cart.

In your custom theme function you then simply return check_plain($element['#value']);

That way there's no qty input box on the cart page at all.

Chris's picture
Thanks very much for this additional approach. It might be that this is an even better way of approaching this task. I've added it to the original article!
Guss's picture

Nice snippet and perfect for my project (selling virtual files) !

Thanks

joel box - Mondial-IT's picture

An alternative would be to use the add to cart button in a view, not showing an input field. (this is standard selectable functionality.)

When clicking the add to cart button the default amount (1) would be added to the cart.

Then use the cart block to disable or change the add to cart button much like stock check does when it notices out of stock for an item.

Anonymous's picture

Worked for me.

Thanks

Jurjen de Vries's picture

Nice hook thanks.

If you are not familiar with hooks like me. A hook is a piece of code that talks with another module (like Ubercart).

To use the hook you have to create your own module in Drupal.
Paste the code from Chris example into your module_filename.module and change the text mymodule to the filename you have given your module.

You can learn to create your own module in the Drupal documentation at http://drupal.org/node/206753

Bayo's picture

Thanks a million. This saved me hours!!

Post new comment

The content of this field is kept private and will not be shown publicly.

New job vacancies button

Categories

Search