9

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 
12

This is how the different methods to get items from an order work:

  • getItems(): returns array of items from loaded order item collection
  • getAllItems(): returns array of all items that are not marked as deleted
  • getAllVisibleItems(): 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 only getItems() is part of the service contract in Magento\Sales\Api\Data\OrderInterface

  • Thanks man but getAllVisibleitems() returning simple and config products together so its not making sense i if following images can help imgur.com/8ADtnUQ imgur.com/LnMMuOM – Sajid Unar Apr 22 '16 at 23:28
  • The image looks correct. Or is this what you want, not what you have? – Fabian Schmengler Apr 23 '16 at 6:10
  • It should have return the items from cart so just two simple products one from config and another as it is ; I did this logic if ($productItem->getProductType() == "simple" && ($productItem->getParentItem())) { continue; } since simple product of config was not returning price – Sajid Unar May 6 '16 at 0:47
1

Best way to get order items is using $order->getAllVisibleItems()

Explained by fschmengler above,

getAllItems(): This function returns array of all items of cart with config simple product also.

You getting three products because you have use $order->getAllItems() you have to use $order->getAllVisibleItems() method to get original product.

You can check more reference from file vendor/magento/module-quote/Model/Quote.php

/**
     * 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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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