Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento $order->getAllItems() return twice the same item

I'm writing an observer that check every item in a order, at some point I get the items

  foreach($order->getAllItems() as $item){
    //do something
    echo $item->getSku();
  }

  //output
  sku-first
  sku-first
  sku-second
  sku-second

but I get twice the same item with the same sku of course, where's the catch? maybe in some configuration file?

like image 335
15 revs, 2 users 97% Avatar asked Oct 24 '11 14:10

15 revs, 2 users 97%


3 Answers

I believe you want to use getAllVisibleItems() instead of getAllItems().

I believe getAllItems gets the configurable along with its associated simple product.

like image 113
Josh Pennington Avatar answered Dec 08 '22 04:12

Josh Pennington


The option getAllVisibleItems don't work

You have to use this code

$_items = $order->getItemsCollection();

 foreach ($_items as $item) {
    if ($item->getParentItem()) continue;
    //do something
    echo $item->getSku();
}
like image 23
lavb Avatar answered Dec 08 '22 04:12

lavb


If getAllVisibleItems() is not working, make sure you are getting it correctly:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
    echo $item->getQty();
}

Source: https://stackoverflow.com/a/5512656/922522

like image 26
Justin Avatar answered Dec 08 '22 06:12

Justin