Retrieve items on Order Magento 2
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
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->load($orderId);
$orderItems = $order->getAllItems();
You done it using objectManager
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