2

Possible Duplicate:
limit items in commerce shopping cart

I`m using drupal commerce for my webshop, i want to limit products in the shopping cart which means when the user has a product in his shopping cart he must not be able to add other products. Is it possible ?

0

1 Answer 1

6

Sure it's possible there are several ways of doing it.

The simplest solution would be to add a custom validation handler to the add_to_cart_form and raise a validation error if there is something in the cart:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MODULE_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
  $form['#validate'][] = 'MODULE_cart_validate';
}

function MODULE_cart_validate($form, &$form_state) {
  $order = commerce_cart_order_load($GLOBALS['user']->uid);
  if ($order) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    if ($order_wrapper->commerce_line_items->value()) {
      form_set_error('', t("You can only have one item in your cart");
    }
  }
}

Another option is to remove other items in the cart when you add a new one. This could probably be done with rules, but is more easy to do with code if you know a bit of Drupal and PHP.

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