5

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;
    }
2
  • Can you post more of your code? Just that one line does not give one much to go on to help you debug this.
    – ProxiBlue
    Dec 9, 2013 at 9:07
  • Hi ProxiBlue, I have added the observer code to the question Dec 9, 2013 at 9:25

2 Answers 2

3

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())

2
  • I used this approach and it works, thanks. origData was indeed overwritten as well. Dec 10, 2013 at 10:41
  • Yes, irogData it will be overwritten when saving. Dec 10, 2013 at 11:06
2

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.

Your Answer

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

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