1

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

1
  • Hi, have You solved this issue? Commented Mar 29, 2018 at 13:53

2 Answers 2

0

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.

1
  • The problem still persists Commented Nov 8, 2017 at 10:06
0

I think your question lacks a bit more context but I can talk about the default Magento checkout process.

If you check the code in Magento, you'll notice this information is set on Checkout Session by this method.

That said, something on your checkout process is probably clearing the checkout session information before the process reaches the success page.

A simple and easier workaround for this is making your payment method set a different value on the Magento session and then grabbing it rather than the default checkout one (getLastRealOrderId). Or you can debug your process with Xdebug by watching the checkout session throughout the process to understand where it's being cleared or modified.

I'd put breakpoints throughout the code wherever there's a call to setLastRealOrderId() to see how it goes.

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.