Drupal – Enable Quantity Field for Ubercart Add to cart form

Update @ 2015-04-27: There is a simpler way to achieve this without coding. Please refer to this comment.

In the Ubercart product page, the product quantity option in the add to cart form is hidden by default. If you want to allow user modifying the product quantity in the order, create a custom module and implement the following hook.

/**
 * Enable quantity field in add to cart form
 */
function <your_module>_form_alter(&$form, &$form_state, $form_id) {
  //print($form_id);
  if(substr($form_id, 0, 28) == 'uc_product_add_to_cart_form_') {
    $options = array();
    for ($i = 1; $i <= 10; $i++) {
      $options[$i] = $i;
    }
    $form['qty'] = array(
      '#type' => 'select',
      '#title' => t('Quantity'),
      '#default_value' => '1',
      '#options' => $options,
    );
  }
}

 

A selection list will be shown with 1 to 10 options in the add to cart form.

Done =)

Reference: Theming Add To Cart form – attributes and quantities

10 thoughts on “Drupal – Enable Quantity Field for Ubercart Add to cart form”

  1. Just stumpled upon this. Thanks for sharring. I need to have the quantity on the catalog page. If I use this hook I get the quantity in the form, but when I submit the form, the qty is set to 1 no matter what i select. Do you have any clues as to why ? qty works fine on the product node view, but not in the catalog view… I am using ajax driven cart…

    Any help much appreciated 🙂
    /T

    Like

  2. Yeah – its the catalog page provided by ubercart, wich is at /catalog – ie : http:///catalog/2

    I just solved this by overriding the submit handler for the uc_catalog_buy_it_now_form.

    /T

    Like

    1. I just realize that actually we can add the quantity field to the add to cart form without coding. Just enable the Display an optional quantity field in the Add to Cart form @ Adminster -< Store administration -< Configuration -< Product Settings.

      If you want to add the quantity box @ the catalog page, just edit the uc_products view as follow.

      Like

      1. Do you mind editing the post itself to reflect this? Google still links to this page and I just spent an hour searching before finding this flag on my own. Cheers.

        Like

  3. Wow, talk about intuitive (NOT!), but that did it for me too. Who would have guessed that “Product: Add to cart form” contained the Quantity field?

    Thanks ykyuen!!!

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.