Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Order Increment Id in Magento

Tags:

php

magento

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

like image 598
doubleplusgood Avatar asked Feb 03 '10 21:02

doubleplusgood


People also ask

How to get order id by increment id in Magento2?

We can use the order interface \Magento\Sales\Model\Spi\OrderResourceInterface to load order by increment id.

What is increment ID in Magento 2?

The default value of the Increment ID is 9 digits in Magento 2. Although the Magento 2 admin can customize the size of the order increment ID in the custom module. The order increment ID can be increased or decreased according to the requirement.

What is Entity ID in Magento?

Entity Id (entity_id column name in table) is the primary key which is used for the table to keep unique id per row and its auto-increment so when a new row is generated/inserted automatically generate latest id.


3 Answers

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();
like image 125
Chris Norton Avatar answered Oct 15 '22 21:10

Chris Norton


$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();
like image 38
Greg Avatar answered Oct 15 '22 23:10

Greg


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

like image 5
Chiragit007 Avatar answered Oct 15 '22 21:10

Chiragit007