Magento : Get invoice id from invoice incrementId

Magento : Get invoice id from invoice incrementId

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : Get invoice id from invoice incrementId

Retrieving Invoice ID from Increment ID in Magento

When developing custom extensions for Magento, you often need to interact with sales orders and invoices programmatically. One common requirement is to retrieve the actual invoice ID (e.g., 1, 2, 3) from the increment ID provided by Magento.

Let’s consider a typical scenario where you have already created an invoice using the sales_order_invoice_api model:

$invoiceId = Mage::getModel('sales/order_invoice_api')
    ->create($order->getIncrementId(), array(), 'Invoice Created', true, true);

The $invoiceId returned by this method is actually the increment ID assigned by Magento for internal tracking purposes. By default, it starts at 100000001 and increments sequentially.

However, you may need to obtain the actual invoice ID in your database (1, 2, 3) for various reasons such as displaying it on the frontend or using it in third-party integrations. To achieve this, you can use Magento’s default models and collections:

// Get the invoice increment ID
$incrementId = $order->getIncrementId();

// Get all invoices from the database
$collection = Mage::getModel('sales/order_invoice')->getCollection();

foreach ($collection as $invoice) {
    // Check if the current invoice's increment ID matches our target
    if ($invoice->getInvoiceIncrementId() == $incrementId) {
        // You have found your invoice; retrieve and proceed with the actual invoice ID
        $actualInvoiceId = $invoice->getEntityId();
        break;
    }
}

In this example, we first get all invoices from the collection. We then iterate through each invoice and compare its increment_id to our target increment ID obtained earlier. Once a match is found, you can proceed with the actual invoice ID (entity_id).

Remember that the increment_id column in the sales_flat_invoice table corresponds to what Magento returns as $invoiceId in your API call.

If you are using Magento 2, the process remains similar but involves slightly different models and collections:

$collection = $order->getInvoiceCollection();

foreach ($collection as $invoice) {
    if ($invoice->getIncrementId() == $incrementId) {
        $actualInvoiceId = $invoice->getEntityId();
        break;
    }
}

In both cases, always ensure you have proper access to the Magento database tables and obey any security and performance considerations when querying data.

For more advanced examples of working with invoices in Magento extensions, check out our guide on Creating Custom Magento Invoices.

This guide provides a thorough walkthrough of creating invoices programmatically while adhering to Magento’s best practices for extension development.

Database API Extensions Orders

Our network