Magento querying sales or order volume

Magento querying sales or order volume

September 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento querying sales or order volume

Magento Sales Volume Querying: Efficient Ways to Retrieve Order Data

As an experienced Magento developer, I often receive queries about retrieving sales volume data for analysis and reporting purposes. In this post, we will explore two primary options for querying order data in Magento and discuss the differences between them.

Option A: Direct Database Query vs Cached Values

Before diving into code examples, let’s first consider whether Magento already caches relevant sales data that you can leverage:

Pre-cached Sales Data in Magento

Magento does maintain some pre-calculated sales data, such as total lifetime sales and average order value. You can retrieve this information using the sales/report_collection model.

For example:

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection(Mage_Core_Model_Resource::DEFAULT_READ_RESOURCE);
$sql = "SELECT * FROM sales_report_collection";
$reportcollection = $readConnection->fetchAll($sql);

However, this pre-cached data might not provide the granularity you need for daily or weekly averages. In such cases, you’ll have to query the order collection directly.

Option B: Order Collection Querying

Magento provides two primary ways to retrieve an order collection:

Method 1: Using Resource Model

$orderCollection = Mage::getResourceModel('sales/order_collection');

Method 2: Using Model

$orderCollection = Mage::getModel('sales/order')->getCollection();

While both methods return the same sales_order collection, they differ in terms of abstraction and performance:

  1. Resource Model (Mage::getResourceModel()): This method is more lightweight and efficient for simple queries as it directly interacts with the database without passing through additional layers.

  2. Model (Mage::getModel()): The model approach offers greater flexibility, allowing you to chain methods like addAttributeToFilter() and apply complex filters or aggregations before fetching data.

Which Method is Faster?

For most use cases, using the resource model method will be sufficient and more performant. However, if you need to apply multiple filters, aggregations, or additional logic, it’s better to use the model approach to leverage its chaining capabilities.

Retrieving Sales Data with Filters

Let’s update our previous code snippet to filter orders based on a date range:

require_once 'app/Mage.php';
Mage::app('default');

$orderTotals = Mage::getModel( 'sales/order' )->getCollection()
    ->addAttributeToFilter( 'status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE))
    ->addFieldToFilter( 'created_at', array(
        'from' => date( 'Y-m-d H:i:s', strtotime('-100 days') ),
        'to'   => now(),
    ))
    ->addAttributeToSelect('grand_total')->getColumnValues('grand_total');

$totalSum = array_sum($orderTotals);
$totalSum = Mage::helper('core')->currency($totalSum, true, false);

echo $totalSum . "\n";

In this example, we use the addFieldToFilter() method to specify a date range for the created_at field and retrieve only orders with STATE_COMPLETE status.

When to Use Each Method

  • If you need to perform simple queries or don’t require complex filtering, using the resource model (Mage::getResourceModel('sales/order_collection')) is recommended.
  • For more advanced use cases involving multiple filters, aggregations, or additional logic, opt for using the model approach (Mage::getModel('sales/order')->getCollection()).

Conclusion

Retrieving sales volume data in Magento can be achieved through direct database queries or by leveraging pre-cached data. Understanding the differences between the resource model and model approaches is crucial to optimizing your data retrieval process.

By considering your specific requirements, you can choose the most efficient method for querying order data and calculating approximate sales volumes in your Magento store.

For more advanced Magento development techniques, consider exploring our Magento Development Guide at Laravel Company.

Cache Database Orders

Our network