Question

I have built an observer which fires at checkout_cart_product_add_after. In the observer I set the quantity like this:

$item->setQty($quantity); 

I would like to fetch the quantity that is already in the cart for that product, so that the quantity for the product gets updated instead or overwritten. Yet when I try to get the product quantity in the cart (Mage::helper('checkout/cart')), I get the "new" quantity, probably because the update quantity code has already fired.

How could I get the cart quantity for the product?

Thanks in advance,

Joost

EDIT:

This is the full code of the observer:

public function modifyPrice(Varien_Event_Observer $obs)
    {

        $item = $obs->getQuoteItem();
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $price = $this->_getPrice($item);
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);

        $quantity = $this->_getQuantity($item);
        $item->setQty($quantity);

        $item->getProduct()->setIsSuperMode(true);
    }

    protected function _getPrice(Mage_Sales_Model_Quote_Item $item)
    {
        $price = 0;

        Mage::app()->getRequest()->getPost();

        $qtydropdownbox = Mage::app()->getRequest()->getParam('qtydropdownbox');
        $prices = $item->getProduct()->getData('qty_dropdownbox_prices');
        $quantities = $item->getProduct()->getData('qty_dropdownbox_quantities');

        $array_prices = explode(';', $prices);
        $array_quantities = explode(';', $quantities);
        $price = $array_prices[$qtydropdownbox] / $array_quantities[$qtydropdownbox];

        return $price;
    }

    protected function _getQuantity(Mage_Sales_Model_Quote_Item $item)
    {
        $quantity = 0;

        Mage::app()->getRequest()->getPost();

        $qty = Mage::app()->getRequest()->getParam('qty');
        $qtydropdownbox = Mage::app()->getRequest()->getParam('qtydropdownbox');
        $quantities = $item->getProduct()->getData('qty_dropdownbox_quantities');

        $array_quantities = explode(';', $quantities);
        $quantity = $array_quantities[$qtydropdownbox] * $qty;

        return $quantity;
    }
Was it helpful?

Solution

I don't understand what you want to achieve, but to get the old quantity you are too late and even origData is overwritten (I think), but check it.

I think the easiest is to hook into quote_item_load_after and just add the qty to read it later like $quoteItem->setMyPersonalQty($quoteItem->getQty())

OTHER TIPS

You can use the event sales_quote_product_add_after which is triggered before the new items are saved, so that you can use $item->getOrigData('qty') to get the original quantity.

The event has one parameter, items which is an array of all single quote items that were generated for this product. For simple products this is always one item, for bundles and configurable products it is the item for the main product and its children, the parent always being the first item.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top