I am creating a payment gateway. When ever the last order has the state/status as complete, getLastRealOrderId() returns null. Is there any work around or a way to solve it ? I am new to magento

  • Hi, have You solved this issue? – Konrad Siamro Mar 29 '18 at 13:53

Use this code in block or in your helper class.

protected $_checkoutSession;
    protected $_orderFactory;
    protected $_scopeConfig;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Framework\View\Element\Context $context
    ) {
        $this->_checkoutSession = $checkoutSession;
        $this->_orderFactory = $orderFactory;
        $this->_scopeConfig = $context->getScopeConfig();

    // Use this method to get ID    
    public function getRealOrderId()
        $lastorderId = $this->_checkoutSession->getLastOrderId();
        return $lastorderId;

    public function getOrder()
        if ($this->_checkoutSession->getLastRealOrderId()) {
             $order = $this->_orderFactory->create()->loadByIncrementId($this->_checkoutSession->getLastRealOrderId());
        return $order;
        return false;

    public function getShippingInfo()
        $order = $this->getOrder();
        if($order) {
            $address = $order->getShippingAddress();    

            return $address;
        return false;


Using this function, you can get the latest order Id. Hope this will help you too.

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.