Get current orderId in magento payment module during checkout

16,455

Your question, as written, is illogical.

There is no order id during checkout. While you're checking out, Magento creates a sales/quote object. This object is used to keep track of quote items. It's not until checkout is complete that a sales/order object is created from the quote.

The reason getLastOrderId is returning empty is because no order has been placed.

Restating your question with the point in the checkout process that you're at, and what you're trying to do with the order id may help someone come up with the piece of information that you don't know you need.

Good luck!


Author's solution:

The problem was solved by first getting quote ID from checkout/session:

Mage::getSingleton('checkout/session')->getQuoteId(); 

and then quote object by quote ID:

$quote = Mage::getModel("sales/quote")->load($quoteId); 
Share:
16,455
KoviNET
Author by

KoviNET

Web developer from Slovenia. Coding mostly in PHP and related technologies for building web sites / services, like JavaScript, Css, HTML, relational databases, etc.

Updated on June 04, 2022

Comments

  • KoviNET
    KoviNET almost 2 years

    I am implementing my own payment module for Magento, where I implemented getCheckoutRedirectUrl() method in my payment model.

    class My_Module_Model_Payment extends Mage_Payment_Model_Method_Abstract {}
    

    This method is supposed to just return URL of payment gateway where user is redirected to, but I should also append current orderId to this URL.

    The problem is am not able to get orderId. I tried solution as already accepted here

    $orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
    

    but I get empty $orderId. So this doesn't seem to work in my method. The call doesn't produce error, so I get an object (for example Mage::getSingleton('checkout/session')->getQuote()->getSubtotal() returns order amount), but orderId is empty.

    I also tried:

    $order = Mage::getModel('sales/order');
    $order->load(Mage::getSingleton('sales/order')->getLastOrderId());
    $orderId = $order->getIncrementId();
    

    which again returns empty $orderId.

    Any Ideas?

  • KoviNET
    KoviNET over 12 years
    It was my guess order doesn't exist yet, thank you for clarifying this to me, I thought orders is already saved to db earlier. But I need some kind of identifier by which I can get order information (such as amount) in a separate independent request (without cookies) which payment gateway makes in background. Maybe I could pass session Id and set session, so I can have access to quote (order in cart)?
  • Alan Storm
    Alan Storm over 12 years
    The quote, and quote items, are persisted to the database as well. That's how magento tracks abandoned carts. I'd concentrate on using the those objects to get at the information you want.
  • KoviNET
    KoviNET over 12 years
    I solved my problem by passing quoteId to payment gateway (Mage::getSingleton('checkout/session')->getQuoteId();) and then I was able to get the quote data in the seperate request: $quote = Mage::getModel("sales/quote")->load($quoteId); Thanks!
  • MACMAN
    MACMAN almost 9 years
    I am in a module trying to capture quote object in observer. But not getting from the event. $evt->getQuote()
  • Alan Storm
    Alan Storm almost 9 years
    @MACMAN it's the passed in observer that has the objects -- try var_dump($observer->getData());