How can I implement a shopping cart limit? 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 ?
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(&amp;$form, &amp;$form_state) {
  $form['#validate'][] = 'MODULE_cart_validate';
}

function MODULE_cart_validate($form, &amp;$form_state) {
  $order = commerce_cart_order_load($GLOBALS['user']-&gt;uid);
  if ($order) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    if ($order_wrapper-&gt;commerce_line_items-&gt;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.