I am trying to get the last order so that I can load the order by increment id but
`$this->_checkoutSession->getLastRealOrder()->getIncrementId()`;
is returning null.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI am trying to get the last order so that I can load the order by increment id but
`$this->_checkoutSession->getLastRealOrder()->getIncrementId()`;
is returning null.
Use this code in your block class or helper:
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.