Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - get order id from increment id

Tags:

magento

How do you get the Order Id from magento Order Increment Id?

I can use the following get the product id from SKU:

$product_id = Mage::getModel('catalog/product')->getIdBySku('ABCD1234');

The above example lets me get just the database entity_id of a product without loading everything. I want to achieve the same for order.

like image 422
Latheesan Avatar asked May 20 '13 13:05

Latheesan


People also ask

How to get order by order id in Magento 2?

To load order from Id using Api Repository method,we can use Magento\Catalog\Api\ProductRepositoryInterface instance and cretae it's object($order). $orderId = 2; $order = $this->orderRepository->get($orderId); echo $order->getId();

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.


2 Answers

You can load an order from the IncrementId.

$orderIncrementId = "1000001";
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
echo $order->getId();
like image 124
Lance Badger Avatar answered Oct 27 '22 20:10

Lance Badger


In a Magento model, the load method can take an optional second argument of what attribute to load by. So, in your case, the following should work:

   $order = Mage::getModel('sales/order')->load($incrementId, 'increment_id');
   $id = $order->getId();

In more complex cases, e.g. where you want to load by a combination of fields, you can load a collection, and get the first element of the collection. In your case, you'd do it like this:

   $order = Mage::getModel('sales/order')->getCollection()
      ->addFieldToFilter('increment_id', $increment_id)
      ->getFirstItem();
like image 44
Laizer Avatar answered Oct 27 '22 21:10

Laizer