16
votes

I'm trying to get the Order Increment Id in Magento, on the success.phtml page so that I can use this for affiliate tracking.

I'm using the following code, but it is giving an error on the second line;

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

The error reads:

Fatal error: Call to a member function getIncrementId() on a non-object on line 34: $LastOrderId = $order->getIncrementId();

I was wondering if anyone has any ideas on how to get the Order Increment Id? This is the reference number seen in the admin, usually something like: #1000123

10

10 Answers

13
votes

If you're specifically doing this on the checkout success page - in success.phtml - then the code to get the order increment ID is already available in the template, since it is displayed to the customer.

You just need the following:

$orderId = $this->getOrderId();

Note that this won't work on other pages so, for those, you'd need to use:

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

$order in your code is the last order ID...as the function name implies. If this isn't the value you want, then use it to load an order, and then use the getter on that:

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

This will work perfectly, I m running this one in my module now.

$last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();

Hope it helps thanks. :)

4
votes

Your call to

Mage::getSingleton('sales/order')

isn't returning an object. Try

var_dump(Mage::getSingleton('sales/order'));

to confirm.

I haven't dived into the checkout code recently, but I'm pretty sure that's because sales/order will get you the order in progress. Once the order's been placed it's no longer in progress.

The "right" way to do this would be to create an observer for one of the events that Magento fires during checkout. The

checkout_onepage_controller_success_action

event should be sufficient, assuming you haven't done too much customization of the checkout process.

There's a terse explaination of how to do this on the Wiki (for a different event)

Once you get your event setup and responding, do a

$event = $observer->getEvent();
var_dump($event->getData());

to see what kind of information you have available. Chances are there's an order object in there which will let you get the ID you're after.

3
votes

I had to use...

$_order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());

While in the success.phtml template. Instead of load() I used loadByIncrementId - then my order object was no longer empty.

3
votes

If you are in admin mode - try this:

$orderModel = Mage::getModel('sales/order'); $orders = $orderModel->getCollection()->setOrder('increment_id', 'DESC')->setPageSize(1)->setCurPage(1); $orderId = $orders->getFirstItem()->getIncrementId();

2
votes

getRealOrderId() appears to return the order number as presented in data grids. getId() will return the internal id of row in the database, which you probably don't want.

2
votes

You can get the increment id using this code snippet:

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

Now you can do an echo to the $Incrementid variable and see the increment id.

I hope this helps.

0
votes
$lastOrderIncrementId = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
-1
votes
$shipmentID = $shipment->increment_id;

$order   = $shipment->getOrder();
$orderID = $order->increment_id;