This is how the different methods to get items from an order work:
This is how the different methods to get items from an order work:
getItems(): returns array of items from loaded order item collectiongetAllItems(): returns array of all items that are not marked as deletedgetAllVisibleItems(): returns array of all items that are not marked as deleted and do not have a parent item
So to get only the configurable product and not its associated product, getAllVisibleItems() is the correct method:
- the single simple item does not have a parent => visible
- the configurable item does not have a parent => visible
- the associated simple item has a parent => not visible
Note that unfortunately, as of magento-2.0 only getItems() is part of the service contract in Magento\Sales\Api\Data\OrderInterface.
/**
* Retrieve quote items array
*
* @return array
*/
public function getAllItems()
{
$items = [];
foreach ($this->getItemsCollection() as $item) {
/** @var \Magento\Quote\Model\ResourceModel\Quote\Item $item */
if (!$item->isDeleted()) {
$items[] = $item;
}
}
return $items;
}
/**
* Get array of all items what can be display directly
*
* @return \Magento\Quote\Model\Quote\Item[]
*/
public function getAllVisibleItems()
{
$items = [];
foreach ($this->getItemsCollection() as $item) {
//echo $item->getId()."<br>";
if (!$item->isDeleted() && !$item->getParentItemId()) {
$items[] = $item;
}
}
return $items;
}