Get current orderId in magento payment module during checkout

KoviNET picture KoviNET · Jan 12, 2012 · Viewed 16.3k times · Source

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?

Answer

Alan Storm picture Alan Storm · Jan 12, 2012

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);