You are here

function uc_restrict_qty_cart_item in Ubercart Restrict Qty 6.2

Same name and namespace in other branches
  1. 5 uc_restrict_qty.module \uc_restrict_qty_cart_item()
  2. 6 uc_restrict_qty.module \uc_restrict_qty_cart_item()

Implementation of hook_cart_item().

File

./uc_restrict_qty.module, line 245
Restrict the quantity on specified products so that only specified quantity may be purchased at a time.

Code

function uc_restrict_qty_cart_item($op, &$item) {
  if ($op == 'load') {

    // If this item has a quantity restriction on it...
    if ($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_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE cart_id = '%s' AND nid = %d AND data = '%s'", $restrict_qty, time(), uc_cart_get_id(), $item->nid, serialize($item->data));
        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');
      }
    }
  }
}