4

In customer account order view page I added a Block and an associated phtml file. Inside this phtml, I would like to load the order to access some informations.

NB: the block is added dynamically and not using XML

I tried in my block :

protected $_order;

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Api\Data\OrderInterface $order
        )
{
    $this->_order = $order;
    parent::__construct($context);
}

and in my pthml :

 $order = $block->_order->load($data['order_id']);

but it results in an error.

If someone could give me a hand please

10

Try following way:

protected $orderRepository;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    array $data = []
){
    $this->orderRepository = $orderRepository;
    parent::__construct($context, $data);
}

public function getOrder($id)
{
    return $this->orderRepository->get($id);
}

From phtml

$order = $block->getOrder($data['order_id']);
  • 3
    It show an error that id is undefined – Jaisa Nov 24 '17 at 6:41
7

Declare \Magento\Sales\Api\OrderRepositoryInterface in construct of your class.

Try following snippet:

protected $orderRepository;

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

Then you can do following:

$order = $this->orderRepository->get($orderId);
$orderIncrementId = $order->getIncrementId(); // To get order incremental id

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.