Magento Get all Product Collection in Admin Controller by Store id's

Magento Get all Product Collection in Admin Controller by Store id's

August 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Get all Product Collection in Admin Controller by Store id's

Getting Store-Wise Product Collections in Magento Admin Controllers

As an experienced Magento developer at Laravel Company, I often encounter scenarios where clients need to fetch product collections based on specific store IDs within admin controllers. This requirement is particularly useful for managing multi-store setups effectively.

The default approach you mentioned - $collection = Mage::getModel('catalog/product')->getCollection(); - fetches all products across all stores in the Magento system. However, when dealing with multiple stores, we need a more targeted approach to filter products by store ID.

Understanding the Store-Product Relationship

In Magento, each product can be associated with one or more stores through its website and visibility settings. When a product is saved, Magento associates it with the current store view context via the store_id field in the catalog_product_entity_int table for attributes like ‘visible_in_sitewide’ and ‘visible_in_catalog’.

Fetching Product Collection by Store ID

The key to fetching products based on store IDs lies in using the correct attribute filters. The setStoreId() method doesn’t directly filter products; it merely sets the current store context, which is used for subsequent operations like saving or loading data.

To filter products by store ID, you need to use the addFieldToFilter() method with a specific attribute that relates to store visibility. In your case, you can filter by the ‘visible_in_sitewide’ attribute:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter('visible_in_sitewide', array('eq'=>1));

This code fetches all products that are visible in the site-wide catalog. However, this still includes products from all stores.

For more targeted filtering by store ID(s), you can use the joinAttribute() and addStoreFilter() methods:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->joinAttribute('visible_in_sitewide', 'catalog_product_entity_int', 'entity_id=entity_id', null, 'inner');
$collection->addStoreFilter(array(2));

In this example, we first join the catalog_product_entity_int table to access the ‘visible_in_sitewide’ attribute. We then use addStoreFilter() with an array containing the store ID (2) you want to filter by.

Handling Multiple Store IDs

If you need products from multiple stores, you can pass an array of store IDs:

$collection->addStoreFilter(array(2, 3));

This fetches all products that are visible in both stores with IDs 2 and 3.

Optimizing Performance

When working with large product collections, it’s essential to optimize your query performance. You can do this by adding index filters for the attributes you’re filtering by:

$collection->addAttributeToFilter('visible_in_sitewide', array('eq'=>1));

This ensures that Magento uses an indexed field for the filter instead of scanning the entire table, significantly improving performance.

Conclusion

Fetching store-wise product collections in Magento admin controllers is a common requirement when managing multi-store setups. By understanding how to use addFieldToFilter() with visibility attributes and combining it with addStoreFilter(), you can effectively retrieve products for specific stores within your admin controllers.

Remember, the key lies in leveraging the correct attribute filters that tie the product to the desired store context. Always test your queries to ensure they return the expected results based on your store’s configuration.

At Laravel Company, we specialize in providing custom Magento solutions tailored to our clients’ unique business requirements. If you have any other questions about Magento development or need expert assistance, feel free to reach out and let us know how we can help!

Catalog Database Admin Configuration

Our network