1

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.

1
  • Do you mind if I answer with a way you could go about finding the solution?
    – Sam
    Commented Aug 2, 2013 at 19:26

2 Answers 2

2

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!

0

While adding products to cart
You can create your own custom module and in that module try rewriting class Mage_Checkout_Model_Cart and override method addProduct($productInfo, $requestInfo=null)

place below code

if($request->getQty() > $product->getStockItem()->getQty() ) {
            $request->setQty($product->getStockItem()->getQty());
        }

inside below if condition

if ($product->getStockItem()) {

new if condition will look something like

if ($product->getStockItem()) {

            // @description : to set quantity to maximum available if requested quantity is greater than maximum available
            if($request->getQty() > $product->getStockItem()->getQty() ) {
                $request->setQty($product->getStockItem()->getQty());
            }

            $minimumQty = $product->getStockItem()->getMinSaleQty();
            //If product was not found in cart and there is set minimal qty for it
            if ($minimumQty && $minimumQty > 0 && $request->getQty() < $minimumQty
                && !$this->getQuote()->hasProductId($productId)
            ){
                $request->setQty($minimumQty);
            }
        }

you can try this and check if it fulfill your requirements.
you can go through http://prezi.com/360sqt1ykfdl/magento-checkout-components/ for some reference

While updating shopping cart
You can try creating an observer for event "checkout_cart_update_items_before" and in this observer you can compare updated request quantity and maximum available quantity and then modify requested quantity accordingly.

Hoping this will be of some help to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.