10

I'm getting the shopping cart using:

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

And then iterating through it using:

foreach ($cart->getAllItems() as $item) { }

But it seems to be returning duplicate items with the same SKUs, but different product ids! On the main site, when I open the cart, however, it shows the single product.

cart: {
    id: 680,
    items: [
        {
            name: "Tori Tank",
            price: "60.0000",
            id: "418",
            sku: "wbk004"
        },
        {
            name: "Tori Tank",
            price: "60.0000",
            id: "286",
            sku: "wbk004"
        }
    ]
}

My question is.. Why is this happening? Shouldn't the parent (configurable) product be showing its own unique Sku?

Also, if I want to add a product to the cart, should I add the configurable product? Or do I add the simple product and Magento handles the rest?

10

According to magento,when a configurable product added at cart then two row are inserted at database. one configurable product cart another is simple product

  • One row have configurable id and simple product Sku and parent item id is null
  • Other row have simple id and simple product sku and parent item id should above row id

As you have using getAllItems() then on for loop you need to check is it has parent item $item->getParentItemId().

foreach ($cart->getAllItems() as $item) {
    / * add this */
    if ($item->getParentItemId()) {
        continue;
    }
........
}
  • You can also used Marius code for user visibility purpose – Amit Bera Mar 31 '15 at 7:49
  • There is a problem though.. I can't get the color, size, etc.. When I use the getAllVisibleItems(). How should I do that? – Pat Mar 31 '15 at 8:44
  • yes it possible by using Product model by Sku,because sku is same for two.. rowss – Amit Bera Mar 31 '15 at 9:15
10

Use $cart->getAllVisibleItems() instead of $cart->getAllItems().
You get duplicates because when adding a configurable product to the cart, magento actually adds 2 products, the simple product and the configurable product but only one is visible for the user.

  • If I add a simple product, it will automatically add the configurable one as well? Or do I need to do it manually? – Pat Mar 31 '15 at 7:32
  • No. When you add a configurable product, there will be 2 in the cart,. – Marius Mar 31 '15 at 8:05
1
<?php
$cart = Mage::getModel('checkout/cart')->getQuote();

foreach ($cart->getAllVisibleItems() as $item) 
{ 
  echo $productName = $item->getProduct()->getName();
  echo $productPrice = $item->getProduct()->getPrice();
  echo $grandTotal = Mage::getModel('checkout/session')->getQuote()->getGrandTotal();
  echo $this->helper('catalog/image')->init($item->getProduct(), 'small_image')->resize(50,50);
}

?>
  • Add code in <?php ?> for all lines – Bhagyavant Apr 18 '17 at 8:22

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.