Magento – Same sku in items of configurable product in order

configurable-productmagento-1.7

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)?

Best Answer

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.)