Using a product factory object, I'm able to create a product, grab a product collection, and fetch the first item of that collection

/* var $productFactory \Magento\Catalog\Model\ProductFactory */
$product = $this->productFactory->create()->getCollection()->getFirstItem();

However, if I attempt to add a store_id to the collection's filter

    $product = $this->productFactory
        ->addFieldToFilter('store_id', 1)

I get the following error

Invalid attribute name: store_id
#0 /Users/alanstorm/Sites/magento-2-dev-docs.dev/magento2/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php(1434): Magento\Eav\Model\Entity\Collection\AbstractCollection->_addAttributeJoin('store_id', 'inner')
#1 /Users/alanstorm/Sites/magento-2-dev-docs.dev/magento2/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php(359): Magento\Eav\Model\Entity\Collection\AbstractCollection->_getAttributeConditionSql('store_id', 1, 'inner')
#2 /Users/alanstorm/Sites/magento-2-dev-docs.dev/magento2/app/code/Magento/Catalog/Model/Resource/Product/Collection.php(1489): Magento\Eav\Model\Entity\Collection\AbstractCollection->addAttributeToFilter('store_id', 1, 'inner')
#3 /Users/alanstorm/Sites/magento-2-dev-docs.dev/magento2/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php(382): Magento\Catalog\Model\Resource\Product\Collection->addAttributeToFilter('store_id', 1)
#63 {main}

The same thing happens if I attempt to use a product repository to filter by store_id (the repositories use the collections under the hood).

Is this a bug? Or has the relationships between stores, websites, and products changed in Magento 2 such that that this is no longer a legitimate query? Both? Neither? Something else?


You can do this with the method addStoreFilter(), see Magento\Catalog\Model\ResourceModel\Product\Collection#addStoreFilter()

the addStoreFilter() function will accept store ID or Store object as a parameter.

EG, to get all products for the current store:

public function getProducts(){
    return $this->collection->addStoreFilter($this->_storeManager->getStore()); 

Hopefully, this helps.

  • Thanks @amitbeta! If you have a moment — do you know if it's possible to create a store filter using product repositories? magento.stackexchange.com/questions/91278/… – Alan Storm Nov 23 '15 at 17:49
  • sure.. i will look – Amit Bera Nov 23 '15 at 17:59
  • @AmitBera, Please can you explain little bit how to use addStoreFilter() to product collection. – user40382 Jul 29 '16 at 6:35

For now this looks like a bug, because there is no possibility to apply store filter with the ProductRepository::getList() method, passing store id as a filter of SearchCriteria.

In the getList implementation, you can find that all the filters from SearchCriteria applied to collection

    foreach ($searchCriteria->getFilterGroups() as $group) {
        $this->addFilterGroupToCollection($group, $collection);

In Magento\Catalog\Model\ProductRepository::addFilterGroupToCollection there is special handling for Category filter, but there is no one for Store.

So, additional condition should be added to Magento\Catalog\Model\ProductRepository::addFilterGroupToCollection which checks whether we have store filter and if we have - set store id for collection, something like:

        if ($filter->getField() == \Magento\Catalog\Model\Product::STORE_ID) {

Created internal bug for this issue, its number is MAGETWO-45950

  • Any news on that? I cannot find a reference to the ticket number on Github. – Fabian Schmengler Jun 9 '16 at 10:56
  • 1
    In Magento 2 products are assign to Websites, not to Stores. So, initial behavior described by Alan is correct, as Product entity doesn't have Store Id link, just link to Website Id. And Internal ticket is about introduction of Extension attribute with ProductWebsiteLinkInterface into the ProductInterface – Igor Minyaylo Jun 10 '16 at 11:21
  • Besides store/website association, doesn't setStore() also specify which store specific attribute values are fetched? Or is this done in a different way now? – Fabian Schmengler Jun 10 '16 at 11:44
  • There are setStoreId/getStoreId methods in Product model implementation, but there are no ones in ProductInterface, so it's not recommended to rely on them in your business logic. – Igor Minyaylo Jun 10 '16 at 11:57
  • For now resolving for StoreView level values (for example, localization of attributes) made by StoreID URL part in REST APIs – Igor Minyaylo Jun 10 '16 at 12:00

If you use custom model with several tables add table_name like: addFieldToFilter('**table_name.**column_name', 1)

  • Could you please share the whole snippet for loading the product collection from my column say entity id, as you have said above – Sushivam Aug 8 '16 at 5:04

1) The class is \Magento\Catalog\Model\ResourceModel\Category\Collection:

/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
$collection = $this->categoryFactory->create()->getCollection()

2) Then the method is $collection->setStoreId(0);

  • P.S. instead of 0 you may put your store id 1, 2, ... – Giedrius Tumelis Mar 15 '17 at 13:22
  • Meta: For some reason star symbol was removed from my message here. – Giedrius Tumelis Mar 15 '17 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.