How can I get the latest order ID when manipulating the success page? What do I need to implement in my Block in order to get access to the Order factory?

1 Answer 1


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.

  • This issues a white page when using, after I've cleared the specific folders in var/ (generation, ...). What could trigger this?
    – Max
    Commented Apr 21, 2017 at 11:40
  • What mode is set for your environment. Please check if its production, change it to development. This will enable you to see the possible errors there. Commented Apr 21, 2017 at 11:47
  • Good catch - however, it is development. Page stays white. Magento 2.0.4.
    – Max
    Commented Apr 21, 2017 at 11:52
  • As I'm extending a Template Block, the context and the data array is missing.
    – Max
    Commented Apr 21, 2017 at 12:03
  • does adding those elements fixes your issue? Commented Apr 21, 2017 at 12:04

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.