I am working on M2 Payment Extension our merchant need us to send items details along with order
Everything works fine but $order->getAllItems();
or $order->getAllVisibleItems();
returning simple and configurable parent products so
if I have 2 products in Cart
- 1 Simple Product
- 1 Associated of configurable product
so instead of getting two products I am getting three ; one simple , associated product and its parent product;
I can try to extract correct products but I am sure it must be some other
/** @var \Magento\Sales\Model\Order $order */
$order = $payment->getOrder();
/**
* Get All Items of Products
*/
$productItems = $order->getAllItems() // returning 3 products where I have 2 in cart
$productItemsTest = $order->getAllVisibleItems(); // returning 3 products where I have 2 in cart
Best Answer
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 itemSo to get only the configurable product and not its associated product,
getAllVisibleItems()
is the correct method:Note that unfortunately, as of magento-2.0 only
getItems()
is part of the service contract inMagento\Sales\Api\Data\OrderInterface