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?
I believe you want to use getAllVisibleItems()
instead of getAllItems()
.
I believe getAllItems gets the configurable along with its associated simple product.
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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With