Magento: Build custom product collection of all configurable products in stock

Magento: Build custom product collection of all configurable products in stock

August 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Build custom product collection of all configurable products in stock

Optimizing Magento: Build a Fast, Efficient Collection of In-Stock Configurable Products

As a seasoned developer working with Magento eCommerce platforms, I frequently encounter performance issues when building product collections using the default methods. The provided script attempts to filter configurable products based on their stock status but suffers from slow execution and high resource consumption.

Understanding the Current Approach

The current script follows these steps:

  1. It creates a collection of all configurable products.
  2. It iterates through each product in the collection, loading individual models.

While this method is straightforward, it has significant drawbacks:

  • High Resource Usage: Loading every product model is expensive and time-consuming, especially for large catalogs.
  • Redundant Operations: The script first loads the collection, then loads each model individually. This repetition increases CPU overhead.

Improving Performance: Filtering Before Loading

To optimize this process, we need to change our approach:

  1. Create a product collection with the required filters (e.g., configurable products).
  2. Apply the in-stock filter directly to the collection before loading any models.
  3. Iterate through the collection and perform necessary operations.

Applying In-Stock Filters

Magento provides the cataloginventory/stock model for applying stock filters. To use it effectively:

  1. First, ensure you have a configured product collection:
$collection = Mage::getResourceModel('catalog/product_collection');
  1. Apply the configurable type filter:
$collection->addAttributeToFilter('type_id', array('eq' => 'configurable'));
  1. Add the in-stock filter to the collection using addInStockFilterToCollection() method:
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
  1. Iterate through the filtered, stock-available products:
foreach ($collection as $_product) {
    // Perform necessary operations on in-stock configurable products
}

Combining Filters: Is Saleable and In Stock

If you need to check both ‘is saleable’ and ‘in stock’, combine the conditions:

  1. Add a condition for is_saleable() while iterating:
foreach ($collection as $_product) {
    if ($_product->isSaleable()) {
        // Perform operations on in-stock, saleable configurable products
    }
}

Conclusion

Optimizing product collection building is crucial for Magento performance, especially for large catalogs. By filtering and loading only necessary products, we can significantly improve script execution speed.

Remember to always test changes thoroughly before deploying them in a production environment.

To learn more about advanced Magento optimization techniques, consider reading our guide on Magento Performance Tuning for comprehensive insights and best practices.

Catalog Performance Configuration Errors

Our network