16

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

Artemix
  • 2,113
  • 2
  • 23
  • 34
doubleplusgood
  • 2,486
  • 11
  • 45
  • 64

10 Answers10

13

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();
Chris Norton
  • 807
  • 5
  • 4
  • 1
    I dont think you should do thinks like this in a view, keep the logic for controller or models – Elzo Valugi Feb 11 '10 at 16:40
  • 2
    @Elzo Magento's isn't a "dumb view" MVC. The intent is PHP classes called blocks expose methods to phtml templates. These methods read directly from Magento model layer to retrieve their data. – Alana Storm Sep 27 '11 at 22:34
  • Please take note, that getOrderId() is marked as deprecated after Magento 1.4.0.1. Better user the checkout session to retrieve to order id. – Martin Rothenberger Aug 02 '12 at 11:12
  • This should not be the accepted answer as it actually provides the incorrect value. The question asks for the order increment id - which is entirely different to the order id. It would be entirely possible/commonplace in the DB for order id and increment id to become out of sync so this could easily go unnoticed during development – BenLeah Jun 27 '16 at 10:57
7

$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();
Greg
  • 10,350
  • 1
  • 26
  • 35
5

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. :)

Chiragit007
  • 1,646
  • 2
  • 16
  • 31
4

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.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • according to my testing, calling Mage::getModel('sales/order') and Mage::getSingleton('sales/order') both return a new Order object rather than the current order. – Jonathan Day May 11 '11 at 11:29
3

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

Matthias Kleine
  • 1,228
  • 17
  • 15
3

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.

Bryce
  • 31
  • 1
2

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.

lucasvm1980
  • 660
  • 1
  • 9
  • 20
2

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.

Dominic
  • 21
  • 1
0
$lastOrderIncrementId = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
Naveenbos
  • 2,532
  • 3
  • 34
  • 60
-1
$shipmentID = $shipment->increment_id;

$order   = $shipment->getOrder();
$orderID = $order->increment_id; 
gsamaras
  • 71,951
  • 46
  • 188
  • 305