6

My objective is to send an order to our backend system when a order is placed within our Magento 2 instance.

The code to send the order object is working. My question is what is the optimum approach to retrieve item data (e.g. price, subtotal, total, sku, etc.).

I'm using the following to retreive the order and items.

$orderId = $this->_order->load($order);
$billing = $orderId->getBillingAddress();
$items = $orderId->getAllItems();
foreach ($items as $item) {
        $itemPrice = $item->getPrice();
        $itemSku = $item->getSku();
}

Is iterating through $items to get price, sku, etc. the best way or is there a Magento2 collection/method I should be using?

Thanks

6
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId);
$orderItems = $order->getAllItems();

You done it using objectManager

  • 14
    Use of object manager is not encouraged in Mage 2 – Slimshadddyyy Apr 4 '16 at 12:00
19

The correct way to load Order object (which will carry the items info in itself) is via Repository objects.

Inject the order repository in your constructor

protected $orderRepository;

public function __construct(
    ...
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    ....
) {
    ....
    $this->orderRepository = $orderRepository;
    ....
}

and use in required method as:

$order = $this->orderRepository->get($orderId);
foreach ($order->getAllItems() as $item) {
// var_dump($item->getData());
}

To know more about Repository Objects: http://alanstorm.com/magento_2_understanding_object_repositories

  • 1
    upvoted for the correct way of using dependency injection! Should have been marked as the answer. – Long M K Nguyễn Nov 30 '18 at 21:54
  • should use $order->getAllVisibleItems() – HaFiz Umer May 15 at 5:07
10

below i code which will help you to get order items (i called in Block class)

<?php
namespace Sugarcode\Test\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    protected $order;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Sales\Model\Order $order,
        array $data = []
    ) {
        $this->order = $order;
        parent::__construct($context, $data);
    }



    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function getOrderItems()
    {
        $order=$this->order->load(1);

        return $order->getItems();

    }
}

if it works accept the answer which will helps others

  • In case others come by and try and get this working but can't, I was not able to load the order using load(1). But i was able to get the order information to load using loadByIncrementId($orderId). – circlesix Sep 27 '16 at 19:53
5

If you use

$order->getAllVisibleItems()

Instead of

$order->getAllItems()

You will get better results


More Explanation :

If you take a look at sales_order_item table you'll see that there are two rows for each item in the order, one of them with useless data and the other one with the data that you'd probably need. Well if you use getAllItems() you'll get those two lines, but if you use getAllVisibleItems() you'll get only the rows with the useful data. I hope this does help you!

  • "Better" how? That's a bit vague. – Mike Apr 23 '18 at 20:19
  • 2
    Sorry for not being more specific, if you take a look at sales_order_item table you'll see that there are two rows for each item in the order, one of them with useless data and the other one with the data that you'd probably need. Well if you use getAllItems() you'll get those two lines, but if you use getAllVisibleItems() you'll get only the rows with the useful data. I hope this does help you! – Char Apr 24 '18 at 13:49

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.