1

I know we can easily limit the max quantity of a given product a customer can purchase per order, but is it possible (natively or even with a plugin) to limit max quantity of a given product per CUSTOMER ??

I don't want to use a coupon nor modify the code: it needs to be a sale price with the help of native or extension functionality.

Magento 1.5.1

Community
  • 1
  • 1
Gaia
  • 2,872
  • 1
  • 41
  • 59

3 Answers3

5

It is not possible native, but you can make a module that will perform such restrictions.

  1. You need to create a resource model, that will retrieve not canceled and not refunded orders for product(s) with particular product id. Actually it just a simple select to sales/order and sales/order_item table. Method of resource model might look like the following:

    public function getPurchasedProductQty(array $productIds, $customerId)
    {
        $select = $this->_getReadAdapter()->select();
        $select
            ->from(array('order_item' => $this->getTable('sales/order_item')),
                      array(
                          'qty' => new Zend_Db_Expr('order_item.ordered_qty - order_item.canceled_qty - order_item.refunded_qty'),
                          'product_id'))
            // Joining order to retrieve info about item and filter out canceled or refunded orders
            ->join(array('order' => $this->getTable('sales/order')),
                   'order.entity_id = order_item.order_id',
                   array())
            // Limit it to the current customer
            ->where('order.customer_id = ?', $customerId)
            // Filter out refunded and canceled orders
            ->where('order.state NOT IN(?)', array(
                Mage_Sales_Model_Order::STATE_CLOSED,
                Mage_Sales_Model_Order::STATE_CANCELED
            ))
            // Add Product Id Condition
            ->where('order_item.product_id IN(?)', $productIds);
    
        return $this->_getReadAdapter()->fetchCol($select);
    }
    
  2. Then when you observe sales_quote_item_collection_products_after_load event you just can place your custom logic with checking the restrictions on products that are going to be used in the cart and remove that ones from loaded collection. This logic you should implement yourself.

Ivan Chepurnyi
  • 9,233
  • 1
  • 43
  • 43
  • Can you please tell how to get Ids of products from `sales_quote_item_collection_products_after_load` event's observer. – Sourabh Dec 22 '14 at 13:22
0

Assuming that you are trying limit the product qty a registered customer who is currently log in can add to their cart.

(This is a one to one relationship, but could easily modify to accomodate many different products and qty per customer)

Create a custom module that will add a field in customer entity, so admin can set the appropriate qty for each customer.

Field name: [ModuleName]_product_id (see Adding attributes to customer entity)

Field name: [ModuleName]_max_cart_qty (see Adding attributes to customer entity)

In (copy files below to your local template folder) and update the qty input field.

/app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml /app/design/frontend/base/default/template/checkout/cart/item/default.phtml

Change

 <input type="text"  class="input-text qty" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" />

to (Add a validation class to make sure the qty is less than or equal )

$addValidationClass = '';
if( Customer is login && ModuleName_product_id == $_product->getId() && [ModuleName]_max_cart_qty > 0){
    $addValidationClass = ' validate-digits-range-1-' . [ModuleName]_max_cart_qty
}

<input type="text"  class="input-text qty<?php echo $addValidationClass; ?>" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" />

If you want to do server-side validation then create a observer for add to cart event, that compare the above logic to the item currently been add to cart

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
-1

Below Extension Will Be help to achieve this

https://www.magentocommerce.com/magento-connect/maximum-order-quantity.html

Edit In this Extension give

Quite often store owners need to restrict the order product quantity with custom message at cart page. This is not possible while using the default admin settings. However, by using this extension you can set the limit for product quantity with custom error message. If maximum quantity limit exceeds the limit then error message will be shown at cart page.

You can set the maximum quantity for each product with custom error message. You can enable/disable globally them using the backend options.