13

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

1
  • Does print_r(orderId->getAllItems()) output all the items? Commented May 24, 2020 at 0:59

4 Answers 4

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

You done it using objectManager

2
  • 19
    Use of object manager is not encouraged in Mage 2 Commented Apr 4, 2016 at 12:00
  • Does print_r(orderId->getAllItems()) output all the items? Commented May 24, 2020 at 1:00
32

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

4
  • 3
    upvoted for the correct way of using dependency injection! Should have been marked as the answer. Commented Nov 30, 2018 at 21:54
  • should use $order->getAllVisibleItems()
    – HaFiz Umer
    Commented May 15, 2019 at 5:07
  • 1
    It depends on your requirement, whether to use getAllVisibleItems() vs getAllItems()
    – MagePsycho
    Commented Jul 24, 2019 at 6:24
  • Does print_r(($order->getAllItems()) output all the items? Commented May 24, 2020 at 1:00
11

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!

3
  • "Better" how? That's a bit vague.
    – Mike
    Commented Apr 23, 2018 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
    Commented Apr 24, 2018 at 13:49
  • Does print_r(orderId->getAllItems()) output all the items? Commented May 24, 2020 at 1:00
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

1
  • 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
    Commented Sep 27, 2016 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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