total sales of each product in magento

total sales of each product in magento

August 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
total sales of each product in magento

Total Sales of Each Product in Magento: A Developer’s Guide

As an experienced developer, you may often need to retrieve the total sales figures for each product in your Magento e-commerce platform. Understanding this process is crucial for tracking inventory turnover, optimizing marketing strategies, and making data-driven decisions.

What You Need to Know About Magento Sales Data

In Magento, the total sales of a product are stored as an attribute called “total_sales”. This attribute is updated automatically whenever a new order is placed through your store’s frontend or admin panel. It represents the cumulative number of times this specific product has been sold.

Retrieving Total Sales Using Magento’s API and MySQL

To fetch the total sales data for all products, you can use either Magento’s API (SOAP or REST) or a direct SQL query on your database. Let me demonstrate both approaches:

Option 1: Using Magento SOAP API

The SOAP API provides a comprehensive set of methods to interact with Magento entities like products, orders, and customers. To get total sales using the SOAP API, follow these steps:

  1. Install the SOAP client library in your PHP environment.
  2. Create an instance of the Varien_Soap_Client class.
  3. Call the catalog_product_list method to fetch all product IDs.
  4. Loop through each product ID and call the catalog_product_info method to retrieve the product details, including “total_sales”.

Here’s a code snippet illustrating this process:

// Initialize SOAP client
$client = new Varien_Soap_Client('http://your-magento-store.com/apisoap/index.php?wsdl');

// Fetch all product IDs
$result = $client->call('catalog_product_list');
$productIds = array_keys($result);

// Loop through each product ID and fetch details
foreach ($productIds as $productId) {
    $productInfo = $client->call('catalog_product_info', array('product_id' => $productId));
    // Access total_sales attribute: $totalSales = $productInfo['data']['total_sales'];
}

Option 2: Direct SQL Query

If you prefer a more direct approach, you can query the Magento database using plain MySQL. The “catalog_product_entity” table stores product-related data, including the “entity_id” (product ID) and “total_sales”.

Here’s an example of fetching total sales using a simple SELECT statement:

SELECT entity_id, total_sales
FROM catalog_product_entity;

This query will return a list of all products, along with their respective total sales figures.

Best Practices for Working with Magento Sales Data

When handling Magento sales data:

  1. Use the API for better performance and security: While direct SQL queries are faster, they expose your database to potential risks. The API provides an abstract layer that adheres to Magento’s data architecture and security rules.

  2. Paginate results when working with large datasets: If you have a vast number of products, fetching all data at once can be memory-intensive. Use pagination in the API or implement LIMIT/OFFSET clauses in your SQL queries to retrieve manageable chunks of data.

  3. Stay up-to-date with Magento versions and changes: The location of sales data may vary slightly across different Magento versions. Always refer to the official documentation for the version you’re using.

Conclusion

Retrieving total sales data for each product is a fundamental task in managing your Magento store. Whether you choose the SOAP API or direct SQL queries, understanding the underlying structure and attributes will help you make informed decisions about inventory management, marketing strategies, and customer analytics.

Remember to optimize your performance by implementing pagination and staying aware of version-specific changes in Magento’s data architecture.

For more advanced techniques like integrating sales data with external systems or generating custom reports, consider exploring the Laravel Company blog for in-depth tutorials and best practices: https://laravelcompany.com/blog.

Happy coding!

Catalog Database Performance API

Our network