Display quantity on category page Magento 2

Display quantity on category page Magento 2

September 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display quantity on category page Magento 2

Optimizing Product Quantity Display on Category Pages in Magento 2

Displaying product quantities on category pages is a common requirement for e-commerce stores. However, when dealing with a large number of products, this can cause performance issues due to the increased load on the server from multiple stock requests. In this post, we’ll explore an optimized approach to display product quantities in Magento 2.

Understanding the Current Approach

The current implementation uses the stockRegistry to fetch the quantity for each product individually:

public function getProductStock($id) {
    return $this->_stockRegistry->getStockItem($id)->getQty();
}

While this works, it generates a separate API call for every product on the category page. For large categories, this can lead to a high number of requests and potentially cause server errors or timeouts.

The Issue with Multiple Stock Requests

The problem lies in the nature of Magento’s stock management system. When you use stockRegistry, it triggers a separate API call for each product’s stock item. If your category has 10,000 products, that’s 10,000 requests being made simultaneously. This can easily overwhelm your server’s capacity and lead to errors.

An Optimized Approach: Batch Processing

To resolve this issue while maintaining performance, you can use a batch processing technique to fetch the stock data in smaller chunks.

Here’s how you can implement this:

  1. Fetch Products in Chunks: Instead of fetching all products at once, fetch them in smaller batches. You can adjust the batch size based on your server’s capacity and the number of products in each category.
$batchSize = 50; // Adjust this value depending on your server's capacity

foreach ($products as $productGroup) {
    foreach (array_chunk($productGroup, $batchSize) as $idsBatch) {
        // Process each batch of IDs
    }
}
  1. Use Indexed Stock Data: Magento 2 provides an indexed stock data model that can be used to fetch multiple stock items simultaneously. This drastically reduces the number of API calls required.
$stockItems = $this->_stockRegistry->getStockItemIds();
foreach ($stockItems as $id => $item) {
    // Process each item
}
  1. Caching: Use Magento’s built-in cache to store stock data for a certain period. This reduces the number of API calls required and improves performance.

Implementing the Solution

To implement this solution, you’ll need to:

  1. Create a new block class that extends Magento\Catalog\Block\Product\ListProduct, similar to your current setup but with the batch processing logic integrated.

  2. Update your XML configuration file to use the new block class in place of the existing one.

  3. Clear your Magento cache and run the store to ensure the changes take effect.

Benefits of This Approach

Adopting this optimized approach provides several benefits:

  • Improved Performance: By reducing the number of simultaneous API calls, you decrease the load on your server, leading to faster page loading times.

  • Lower Server Load: With fewer requests being made at once, you reduce the risk of overloading your server and experiencing errors or timeouts.

  • Scalability: This approach allows your store to handle a larger number of products in each category without compromising performance.

Conclusion

Displaying product quantities on category pages is a critical feature for many e-commerce stores. However, when dealing with large numbers of products, it’s essential to optimize the process to ensure performance doesn’t suffer due to excessive server load.

By using batch processing and the indexed stock data model provided by Magento 2, you can significantly improve the efficiency of this process. This not only provides a better user experience but also ensures your store remains performant as it grows in size.

Remember that the key is to find a balance between fetching enough data to display on the page while minimizing the server load. Regularly review and adjust your batch sizes based on your store’s performance under different loads.

For more detailed guides on Magento 2 optimization, feel free to explore our comprehensive tutorials at Laravel Company.

Magento 2 Catalog Performance API

Our network