How to get all orders and all invoices from php in Magento?

How to get all orders and all invoices from php in Magento?

July 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get all orders and all invoices from php in Magento?

Accessing All Orders and Invoices from PHP in Magento

In Magento, retrieving all orders and invoices programmatically using PHP is a common requirement for many extensions and custom integrations. This process involves interacting with the Magento API or database directly to fetch the necessary data.

Retrieving All Orders

To get all orders from your Magento store, you can use the sales_order_collection model. Here’s an example of how to fetch all orders using PHP:

<?php
require_once 'app/Mage.php';
Mage::init();
$collection = Mage::getModel('sales/order')->getCollection();

foreach ($collection as $order) {
    echo "Order ID: {$order->getIncrementId()}\n";
    echo "Created At: {$order->getCreatedAt()}\n";
}
?>

In this example, we first initialize the Magento application. Then, we create a new sales_order collection object and loop through each record in the collection to display the order ID and creation date.

Retrieving All Invoices

To fetch all invoices related to orders, you can use the sales_order_invoice_collection model. Here’s how to get all invoices using PHP:

<?php
require_once 'app/Mage.php';
Mage::init();
$collection = Mage::getModel('sales/order_invoice')->getCollection();

foreach ($collection as $invoice) {
    echo "Invoice ID: {$invoice->getIncrementId()}\n";
    echo "Order ID: {$invoice->getOrder()->getIncrementId()}\n";
    echo "Created At: {$invoice->getCreatedAt()}\n";
}
?>

In this case, we get the sales_order_invoice collection and iterate through each invoice record. For each invoice, we display the invoice ID, the associated order ID, and the creation date.

Best Practices

When working with Magento’s API or database directly, it’s important to follow these best practices:

  1. Always initialize the Magento application before interacting with models.
  2. Use collection objects to fetch data in a paginated manner for large datasets.
  3. Apply filters and sorting options to optimize performance when dealing with many records.
  4. Ensure you have proper permissions and access rights to execute API requests or database queries.

Conclusion

Retrieving all orders and invoices from PHP in Magento is a straightforward process that allows you to fetch the required data and perform further operations programmatically. By using the appropriate collection models and iterating through the records, you can efficiently access and manipulate order and invoice data for your custom integrations or extensions.

Remember, it’s crucial to adhere to best practices when working with Magento’s API or database directly to ensure optimal performance and security.

To learn more about Magento development and best practices, visit Laravel Company for comprehensive guides and resources.

Database API Extensions Orders

Our network