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.
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();
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.
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.
You can load an order from the IncrementId.
$orderIncrementId = "1000001";
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
echo $order->getId();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With