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

5 Answers 5

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);
    }
1

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

2
  • Hey Fabian, getAllItems is a subset of getItemsCollection. It just excludes items where ->isDeleted() is true
    – Richard
    Dec 1, 2014 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. Dec 1, 2014 at 11:50
1

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

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 and acknowledge you have read our privacy policy.

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