1

I am trying to get Order detail on success page by making a custom module and in my block file code is

/**
 * @var \Magento\Checkout\Model\Session
 */
protected $checkoutSession;

/**
 * @var int
 */
protected $oderId;

/**
 * @var array
 */
protected $lastOrder;

/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Checkout\Model\Session $checkoutSession
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\OrderFactory $orderFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->checkoutSession = $checkoutSession;
    $this->_orderFactory = $orderFactory;
}

protected function _prepareLayout()
{
    $lastOrder = $this->checkoutSession->getLastRealOrder();
    $this->orderId = $lastOrder->getIncrementId();
    $this->lastOrder = $lastOrder->getData();
    return parent::_prepareLayout();
}

public function getOrderId()
{
    return $this->orderId;
}


public function getSubtotal()
{
    /** @var \Magento\Sales\Model\Order $order */
    $order = $this->_orderFactory->create()->load($this->orderId);
    return $order->getSubtotal();
}

public function getDiscountAmount()
{
    /** @var \Magento\Sales\Model\Order $order */
    $order = $this->_orderFactory->create()->load($this->orderId);
    return $order->getDiscountAmount();
}

But after place order and on success page getting error

Uncaught TypeError: Argument 3 passed to CompanyName\Modulename\Block\successpagedata::__construct() must be an instance of Magento\Sales\Model\OrderFactory, array given, called in /vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93

I checked code many times but not getting any issue.Can anybody let me know what I am doing wrong.Please help me.

  • Did you run setup:upgrade and set:di:compile commands afterwards?? – Yogesh Agarwal Sep 15 '17 at 9:23
  • Yes,module is working but issue is in code syntax,it seems something is missing. – akgola Sep 15 '17 at 9:23
1

I'd suggest not to inject OrderFactory and use Service Contracts instead.

Inject Magento\Sales\Api\OrderRepositoryInterface and use get($id) method to load any order by id.

EDIT:

/**
 * @var \Magento\Checkout\Model\Session
 */
protected $checkoutSession;

/**
 * @var int
 */
protected $oderId;

/**
 * @var array
 */
protected $lastOrder;

/**
 * @var \Magento\Sales\Api\OrderRepositoryInterface
 */
protected $orderRepository;

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

protected function _prepareLayout()
{
    $lastOrder = $this->checkoutSession->getLastRealOrder();
    $this->orderId = $lastOrder->getIncrementId();
    $this->lastOrder = $lastOrder->getData();
    return parent::_prepareLayout();
}

public function getOrderId()
{
    return $this->orderId;
}


public function getSubtotal()
{
    /** @var \Magento\Sales\Model\Order $order */
    $order = $this->orderRepository->get($this->orderId);
    return $order->getSubtotal();
}

public function getDiscountAmount()
{
    /** @var \Magento\Sales\Model\Order $order */
    $order = $this->orderRepository->get($this->orderId);
    return $order->getDiscountAmount();
}
  • Can you please give an example? – akgola Sep 15 '17 at 9:29
  • This is being used in vendor/magento/module-checkout/Block/Registration.php – Yogesh Agarwal Sep 15 '17 at 9:33
  • Please update code by injecting Magento\Sales\Api\OrderRepositoryInterface ,actually I don't have more idea. – akgola Sep 15 '17 at 9:35
  • Check my edited asnwer. – Yogesh Agarwal Sep 15 '17 at 9:44
  • Still same issue :__construct() must implement interface Magento\Sales\Api\OrderRepositoryInterface – akgola Sep 15 '17 at 9:49
0

I found an alternate solution for you here Or if you want to fix your code. I think you need to pass arguments in your public __construct also. Or you can use checkout session for get last order on success page. Use this code in block.

  • Nothing is working for me. blank page after trying<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId()); echo "<pre>";print_r($orderData->getData()); //echo $orderData->getData('shipping_description'); //shipping_description ?> – akgola Sep 15 '17 at 11:02

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.