get data from sales order and sales order item in magento

get data from sales order and sales order item in magento

July 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
get data from sales order and sales order item in magento

Retrieving Data from Sales Orders and Order Items in Magento

In this post, we’ll guide you through retrieving specific data from sales orders and order items in Magento using PHP. Nestor has already made some progress and we’ll build upon that to fetch the required information.

Understanding Magento’s Object Model Structure

Magento follows an object model structure where each entity (like order or customer) is represented by a model class. To retrieve data, you typically create a collection of these models and filter them based on your needs.

The sales/order model represents the sales orders in your Magento store, while sales/order_item represents individual items within those orders.

Fetching Customer Information from Sales Orders

Nestor’s current code is almost correct but doesn’t fetch all required data points. Let’s modify it to include customer name, email, and order number:

<?php
set_time_limit(0);
require_once '../app/Mage.php';
Mage::app('uk');

$collection = Mage::getModel('sales/order')
    ->getCollection()
    ->addFieldToFilter('store_id', Mage::app()->getStore()->getId())
    ->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE)
    ->addAttributeToSelect(array('customer_name', 'email', 'increment_id'))
    ->addAttributeToSort('created_at','DESC');

foreach ($collection as $order) {
    echo $order->getCustomerName() . "\t" .
         $order->getCustomerEmail() . "\t" .
         $order->getIncrementId() . "\t" .
         $order->getCreatedAtDate()->format('Y-m-d H:i:s') . "\r\n";
}
?>

In this code:

  • We select the required customer fields using addAttributeToSelect.
  • $increment_id is used to get the order number.
  • $created_at_date is formatted into a readable date-time format.

Fetching Product Information from Order Items

Now, let’s fetch product information like SKU and description. We’ll need to join with sales/order_item:

<?php
require_once '../app/Mage.php';
Mage::app('uk');

$collection = Mage::getModel('sales/order')
    ->getCollection()
    ->addFieldToFilter('store_id', Mage::app()->getStore()->getId())
    ->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE)
    ->join(
        'order_item',
        'main_table.entity_id = order_item.parent_id'
    )
    ->addAttributeToSelect(array('customer_name', 'email', 'increment_id', 'name', 'sku'));

foreach ($collection as $order) {
    echo $order->getCustomerName() . "\t" .
         $order->getCustomerEmail() . "\t" .
         $order->getIncrementId() . "\t" .
         $order->getName() . "\t"  // Product name
         $order->getSku() . "\r\n";
}
?>

In this code:

  • We use the join method to link with the sales/order_item model.
  • $name is used to get the product name and $sku to get the SKU.

Filtering by Current Date

To filter orders from the current date, you can add a condition:

->addAttributeToFilter(
    'created_at',
    array('from' => Mage::getModel('core/date')->date('Y-m-d'))
)

This will fetch only today’s complete orders.

Conclusion and Further Resources

In this post, we’ve shown how to retrieve customer information, product details, and order numbers from Magento. You can further customize the code to suit your specific needs.

Remember, when working with Magento, it’s important to understand its object model structure and use its built-in methods for data retrieval.

For more advanced techniques in Magento development, consider checking out our guide on developing custom extensions. It provides a comprehensive overview of the process.

Happy coding!

Email Orders Customers

Our network