0

My requirement is if the user updates any product qty in cart page then it should check the currently available qty of Product if it's less then the update qty then it should display the message that product qty is not available.

I'm using checkout_cart_update_items_before event.

But here I'm not getting the associated product but getting parent product.

Can anyone suggest me what should I do so?

My code seems not currently working properly so doesn't make any sense to share it here.

EDIT:

Following is my code, Which now showing the message if Product available qty is 1 and user try to add Product Qty more then 1.

public function checkQty(Varien_Event_Observer $observer)
    {
        $cart = $observer->getEvent()->getCart();
        $info = $observer->getEvent()->getInfo();

        foreach ($cart->getProductIds() as $key => $value) {
            $product = $this->_getProduct($value);
            if($product->getStockItem()->getStockQty() != ""){
                if($this->suggestItemsQty($info) > $product->getStockItem()->getStockQty() ){
                    $quote = Mage::getSingleton('checkout/session')->getQuote();
                    $cartItems = $quote->getAllVisibleItems();
                    foreach ($cartItems as $item) {
                        if($product->getId()==$item->getId())
                            $item->setQty($product->getStockItem()->getStockQty());
                    }
                    Mage::throwException(Mage::helper('checkout')->__('The requested quantity is not available'));
                    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
                    exit;
                }
            }
        }

        return $this;
    }
    protected function _getProduct($productInfo)
    {
        $product = null;
        if ($productInfo instanceof Mage_Catalog_Model_Product) {
            $product = $productInfo;
        } elseif (is_int($productInfo) || is_string($productInfo)) {
            $product = Mage::getModel('catalog/product')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->load($productInfo);
        }
        $currentWebsiteId = Mage::app()->getStore()->getWebsiteId();
        if (!$product
            || !$product->getId()
            || !is_array($product->getWebsiteIds())
            || !in_array($currentWebsiteId, $product->getWebsiteIds())
        ) {
            Mage::throwException(Mage::helper('checkout')->__('The product could not be found.'));
        }
        return $product;
    }
    public function suggestItemsQty($data)
    {
        foreach ($data as $itemId => $itemInfo) {
            return $data[$itemId]['before_suggest_qty'];
        }
    }
    public function getCheckoutSession()
    {
        return Mage::getSingleton('checkout/session');
    }
  • "My code seems not currently working properly so doesn't make any sense to share it here." it's exactly the other way round ;) Share your current state and we can try to find a solution together with you. And some additional questions: what do you mean by "if qty is less than the update qty"? Does it mean the stock would become below 0 (out of stock) if I purchase this qty or is it some kind of threshold that is to be respected? Also, are you talking about configurable products? In general, when your products are stock managed the out-of-stock-message is something Magento can do by default. – Christoph Farnleitner Jan 23 '18 at 11:41
  • @ChristophFarnleitner I've edited question,Thanks. – Kaushal Suthar Jan 23 '18 at 11:47
  • So if this is showing the message already, what do you want to achieve else? – Christoph Farnleitner Jan 23 '18 at 11:55
  • It's not updating qty yet in the cart. – Kaushal Suthar Jan 23 '18 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.