Magento – How to Get Product Simple Qty in Cart

cartconfigurable-productproductqty-increment

I am trying to get quantity of simple products in the cart but it only returns the configurable quantity.

How can I get product simple qty in cart?

I am using this:

$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getQuote();

foreach ($cart->getItemsCollection() as $item) {
        if($item->getProduct()->getData('type_id') == 'simple'){
            $theqty = $this->escapeHtml($item->getQty());
        }
    }

Best Answer

Here, this should do what you need, you don't need to access the product object, and it's only the price you'd need from the configurable product line in the quote.

$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
$valor = [];

foreach ($quote->getAllItems() as $item) {
        if ('simple' != $item->getProductType()) continue;
        $valor[]= array (
                'id' => $item->getSku(),
                'quantity' => $item->getQty(),
                'price' => $item->getParentItemId() ? $item->getParentItem()->getPrice() : $item->getPrice()
        );
}

$valor_json = json_encode($valor);