4

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());
        }
    }
  • You haven't really said what you want to get, and your example doesn't really help. – Richard Dec 1 '14 at 11:42
6

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);
2
$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getQuote();
$ids = array();

foreach ($cart->getAllItems() as $item) {
        $sku = $item->getProduct()->getSku();
        $preco = $item->getProduct()->getPrice();
        $theqty = $this->escapeHtml($item->getQty());
        if($item->getProduct()->getData('type_id') == 'configurable'){
            $sku = Mage::getModel('catalog/product')->load($item->getProductId());
            $valor .= '{ id: "'.$sku->getSku().'", price:'.$preco.', quantity: '.$theqty .'},';
            $sku = $item->getProduct()->getSku();
        }elseif($item->getProduct()->getData('type_id') == 'simple'){
            if (!(in_array($sku, $ids))) {
                $valor .= '{ id: "'.$sku.'", price:'.$preco.', quantity: '.$theqty .'},';
            }
        }
        array_push($ids, $sku);
    }
2

You can get product quantity without looping all items:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$product = Mage::getModel('catalog/product')->load($product_id);
$_item = $quote->getItemByProduct($product);
$qty = $_item->getQty();
1

use $cart->getAllItems() to get the simple items too.

  • Hey Fabian, getAllItems is a subset of getItemsCollection. It just excludes items where ->isDeleted() is true – Richard Dec 1 '14 at 11:47
  • Nothing change, but i try something here thats work, now i need get the sku of configurable products, in cart "$sku = $item->getSku();" only return the simple sku. – user3841024 Dec 1 '14 at 11:50
0

You can get product quantity without looping all items:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$_item = $quote->getItemByProduct($product);
$qty = $_item->getQty();

Your Answer

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

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