After I set "Quantity limit": 5.

  1. When cart is empty, I add 10 items. the Quantity restriction is not working. There are 10 items in Cart
  2. When there is 1 or more items in cart, I add 10 items. It works fine. I will get warning in cart and adjust the quantity to limit (You may only add 5 items to your cart. Quantity has been restricted.)
  3. When I update in cart, It works fine. I can't update the quantity more than limit.

Drupal 7.41 + Ubercart 7.x-3.9

Comments

sevenfish created an issue. See original summary.

sevenfish’s picture

Issue summary: View changes
afagioli’s picture

Noticed that. It actually makes sense since this module release works on the hook_uc_cart_item_update(), while we need the hook_uc_cart_item_insert() also.

This is my suggestion in uc_restrict_qty.module:

/**
 * Implements of hook_uc_cart_item_insert().
 */

function uc_restrict_qty_uc_cart_item_insert($item) {
  drupal_set_message(t('An item was added to your cart'));
  // Set a title for the new item
  $item->title = "Some default title";
  uc_restrict_qty_do_check ($item) ;
}


/**
 * Implements of hook_uc_cart_item_update().
 */
function uc_restrict_qty_uc_cart_item_update($item) {
  drupal_set_message(t('An item was updated to your cart'));
  uc_restrict_qty_do_check ($item) ;
}


function uc_restrict_qty_do_check ($item)  {
  // Check if this item has a quantity restriction on it.
  if (isset($item->data['restrict_qty']['qty']) && $item->data['restrict_qty']['qty'] > 0) {
    $is_lifetime = isset($item->data['restrict_qty']['lifetime']) && $item->data['restrict_qty']['lifetime'];
    $restrict_qty = $is_lifetime ? $item->data['restrict_qty']['rest'] : $item->data['restrict_qty']['qty'];
    if ($item->qty > $restrict_qty) {
      // Reduce the quantity to 1 if necessary.
      $item->qty = $restrict_qty;
      db_update('uc_cart_products')
              ->fields(array('qty' => $restrict_qty, 'changed' => time()))
              ->condition('cart_id', uc_cart_get_id())
              ->condition('nid', $item->nid)
              ->condition('data', serialize($item->data))
              ->execute();
      drupal_set_message(format_plural($restrict_qty, 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $item->title)), 'warning');
    }
  }
}

Hope will help

jn2’s picture

Is anyone else working on this? Seems a serious bug in my client's use case.

A digital product is restricted to one item, using this module. However, the Add to cart button for the item can be clicked any number of times with no warning. In the cart, the price is incremented for the number of times the button was clicked, but the Qty always shows as one. Very confusing to customers. The only way to fix it is to delete and readd the item.

The code in #3 looks like movement in the right direction.