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.

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

2 Answers 2

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();
}
8
  • Can you please give an example?
    – akgola
    Sep 15, 2017 at 9:29
  • This is being used in vendor/magento/module-checkout/Block/Registration.php Sep 15, 2017 at 9:33
  • Please update code by injecting Magento\Sales\Api\OrderRepositoryInterface ,actually I don't have more idea.
    – akgola
    Sep 15, 2017 at 9:35
  • Check my edited asnwer. Sep 15, 2017 at 9:44
  • Still same issue :__construct() must implement interface Magento\Sales\Api\OrderRepositoryInterface
    – akgola
    Sep 15, 2017 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.

1
  • 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, 2017 at 11:02

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.