Magento – How to get order details in a Magento2 custom controller action

magento2orderssales-order

I want to get the customer order details in a custom controller in Magento2.
I have created following directory structure and a basic controller.

I have also registered it by following command.

php bin/magento setup:upgrade

.

app
   code 
       Muk
          Frontcontrollerpage
                            Block
                            Controller
                                    Index
                                        Index.php
                            etc
                            view
                            registration.php

I have following code in Index.php

<?php

namespace Muk\Frontcontrollerpage\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action 
{
    protected $resultPageFactory;
    /**
     * @param \Magento\Framework\App\Action\Context $context
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    )
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        return $this->resultPageFactory->create(); // How do I get customer order detail(total, items (sku, item_id, price)) using the order Id?
    }
} 

How do I get customer order detail(total, items (sku, item_id, price)) using the order Id?

Best Answer

You'll need to make the following modifications to get customer order details in your Magento 2 controller.

namespace Muk\Frontcontrollerpage\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

// You need this so you can assign \M\S\M\Order in your constructor.
protected $_order;

protected $resultPageFactory;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Model\Order $_order, // <-- new line 
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
    parent::__construct($context);
    $this->_order = $_order; // <-- new line
    $this->resultPageFactory = $resultPageFactory;
}


public function execute()
{

    $_orderId = 1; 
    // Order Id here or the variable that you plan on using 
    // to specify it. You could pass it by using: 
    // $this->getRequest()->getParam('id', 0)

    /** @var $_order \Magento\Sales\Model\Order  */
    $_order = $this->_order->load($_orderId);

    // Get all items from your order
    $_items = $_order->getAllItems();

    // Iterate through all of the items in the order
    foreach ($_items as $_item) {

        // Get sku, item_id and price
        $_orderItems[] = [
            'sku' => $_item->getSku(),
            'item_id' => $_item->getId(),
            'price' => $_item->getPrice(),
        ];

    }

    //
    // The $_orderItems array will contain all of the items in the order.
    //
    // Not sure what you plan on doing with it but hopefully this will
    // help get you closer to your goal.
    //

    return $this->resultPageFactory->create(); 
}