Is there a module or a detailed how-to out there that can help with creating a form to add multiple products to the cart at once for Drupal 7, Ubercart 3.0?


One way of doing it is by using the Views Bulk Operations module. There is a patch which has recently been committed and offers the "Add to cart" action over here: http://drupal.org/node/1553700

Once your view is built (fields only) you can add a Content VBO field and choose the Add to cart action from there. In theory, redirection to the cart can be done by including the ?destination=cart parameter when calling the View which should be honored once the form is submitted. However, at the moment this appears to only add one item and redirecting rather than all items that have been selected.

  • The patch has been integrated into the VBO module and you just need to select it in views, no need to patch anymore.
    – DrCord
    Feb 20 '14 at 15:56

This should help you. The code as it is below can be put in a template file or function. However when using it in a function you probably want to return the form and add it rendered to some html.

$form = drupal_get_form('multi_add_form');    

function multi_add_form() {
  $form = array();

  // Make the products array in the form a tree.
  $form['products'] = array(
    '#tree' => TRUE,

  // Use an array of product nids to build the form.
  $nids = array(1, 2);

  // Add a quantity field for each product. from 0 to 10
  foreach ($nids as $nid) {
    $node = node_load($nid);

       $x = 0;
   $options = array();
   while ($x <= 10) {
      $options[$x] = $x;

    $form['products'][$nid]['qty'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#title' => t('Amount'),
      '#default_value' => 0,

  // Add an add to cart button.
  $form['add_to_cart'] = array(
    '#type' => 'submit',
    '#value' => t('Add to cart'),

  return $form;

function multi_add_form_submit($form, &$form_state) {
  // Loop through the products array.
  foreach ($form_state['values']['products'] as $key => $value) {
    // The key is the product nid, the value holds the qty.
    if ($value['qty'] > 0) {
      // Add the item to the cart if the quantity was increased.
      uc_cart_add_item($key, $value['qty']);

  // Display a message and redirect to the cart.
  drupal_set_message(t('Your cart has been updated.'));

  $form_state['redirect'] = 'cart';

print drupal_render($form);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.