1

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?

2
  • you need to detail for single order or want multiple order detail? Dec 31, 2016 at 4:56
  • I want single order details
    – Mukesh
    Dec 31, 2016 at 12:37

1 Answer 1

4

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(); 
}

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.