Magento – Update quantity of products in cart to the max available if quantity entered is greater than

cartmagento-1.7

So we've implemented a modification to the product quantity update in cart based on this solution from Demac Media.

If the customer tries to update the quantity of a product in cart that is greater than the available quantity then the idea of this modification is to set the quantity to the maximum available for that product.

Copy…

/app/code/core/Mage/CatalogInventory/Model/Stock/Item.php

To…

/app/code/local/Mage/CatalogInventory/Model/Stock/Item.php

Modify function checkQuoteItemQty($qty, $summaryQty, $origQty = 0)

Find on line 589 (Magento 1.7), change…

if (!$this->checkQty($summaryQty) || !$this->checkQty($qty)) {
        $message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available.', $this->getProductName());
        $result->setHasError(true)
            ->setMessage($message)
            ->setQuoteMessage($message)
            ->setQuoteMessageIndex('qty');
        return $result;

To…

if (!$this->checkQty($summaryQty) || !$this->checkQty($qty)) {
        $message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available. However, we have added the available quantity to your basket.', $this->getProductName());
        $result->setHasError(false)
    ->setOrigQty($this->getQty())
            ->setMessage($message)
            ->setQuoteMessage($message)
            ->setQuoteMessageIndex('qty');
        return $result;

This is working great for products without any options, but if a associated product of a configurable is added to the cart, updating the quantity to one of which is greater than the available in inventory continues to yield the error message and doesn't update to the maximum available.

Any idea's on why the function modification above would not be working for associated products in the cart? Thanks in advance.

Best Answer

You may want to take a look at getBuyRequest in app/code/core/Mage/Sales/Model/Quote/Item.php.

This method appears to be used to pull the product options, and then add them to the quote.

However, you would also want to modify the setProduct method as well, so that way you can setQty() on the Product being saved to the model to reference later in buyRequest.

You could do something like:

->setQty(Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty())

in setProduct().

I wouldn't recommend doing these things in app/code/local/Mage overrides though, it would definitely be better to create a module for these things. In fact, you could accomplish this through an Observer event, listening for sales_quote_item_save_before and do something similar to:

public function checkQuoteItemQty($obs) {
    $event = $obs->getEvent();
    $item = $event->getDataObject();

    $product = Mage::getModel('catalog/product')->load($item->getProduct()->getProductId());
    $itemInventory = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);

    if ($item->getQty() > $itemInventory->getQty()) {
        $item->setQty($itemInventory->getQty());
    }

    return $this;
}

Hope this helps!

Related Topic