0

An order with a configurable product.

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

There're two items. But they have same sku! How did happen? And anyway for each item get it own sku (from configurable and its child product)?

1 Answer 1

3

In Magento when you add configurable products into cart it would have the same sku as simple product that was actually added. Anyway every product should have its own unique SKU (or it can be generated dynamically for bundle products for example) More info can be found here

To avoid this situation you need to use getAllVisibleItems method instead. So your code will be

foreach  ($order->getAllVisibleItems() as $item) {
   echo $item->getSku();
}

The difference between them is that getAllVisibleItems() has an additional check inside the method for every item of an order !$item->getParentItemId() It tests if the product in order has a parent product (it will give you a result without simple products for configurable, bundle etc.)

2
  • Thanks. I know about getAllVisibleItems(), but could I modify magento core to show both Skus, or any hack, maybe?
    – Lan Nguyen
    Mar 30, 2013 at 13:18
  • You seem to be asking a different thing now. The Answer actually covers your Question excellently. Consider accepting it.
    – Meetai.com
    May 20, 2014 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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