0

As I'm new to magento 2 I can't anble to trigger out the difference between getOrderId and getLastRealOrder function ?

How to get product id using getLastRealOrder() function?

4 Answers 4

0

getOrderId() will give Order Id and getLastRealOrder() wil give that last placed order data object

0

Actually getLastRealOrder() loads order by using last increment id for the session, which is unique id but not primary key of any sales_order_* tables. As you may see the implementation.

public function getLastRealOrder()
 {
    $orderId = $this->getLastRealOrderId();
    if ($this->_order !== null && $orderId == $this->_order->getIncrementId()) {
        return $this->_order;
    }
    $this->_order = $this->_orderFactory->create();
    if ($orderId) {
        $this->_order->loadByIncrementId($orderId);
    }
    return $this->_order;
}

Whereas getOrderId() function is used to get order id of the particular order in various contexts.

0

getOrderId(); //Returns order Id

getLastRealOrder(); //Get order instance based on last order ID

0

In Magento 2, the getOrderId() function and the getLastRealOrder() function are used to retrieve order information, but they work slightly differently.

The getOrderId() function is a method of the \Magento\Checkout\Model\Session class

, and it is used to retrieve the order ID of the last order that was placed. The method retrieves the order ID from the session data and returns it as an integer. For example, you could use the getOrderId() method to retrieve the order ID of the last order placed by a customer, and then use that ID to load the order object and display order details to the customer.

$orderId = $this->checkoutSession->getLastOrderId();
$order = $this->orderFactory->create()->load($orderId);

On the other hand, the getLastRealOrder() function is a method of the \Magento\Sales\Model\Order class and it is used to retrieve the last real order object that was placed by the customer. This method returns an instance of the \Magento\Sales\Model\Order class, which contains all of the information about the order, including the order items, billing and shipping addresses, and order totals.

$order = $this->_order->load($this->_checkoutSession->getLastOrderId());

So in short, the main difference between these two functions is that getOrderId() retrieves the order ID as integer and getLastRealOrder() retrieves the complete order object, which you can use to access various data about the order.

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.