3

How do I filter a product collection so that it includes only products in store 2 that are not in store 3. It can include any other stores as long it is in store 2 but not in store 3.

product->storeids() = 2,1 (yes)
product->storeids() = 2 (yes)
product->storeids() = 1,2,3 (no)
product->storeids() = 3 (no)
product->storeids() = 1 (no)
1

2 Answers 2

7

A little more about the concept: A product does not "belong to a store". If you want to exclude it from display in a store view, the most common method imho is to set its visibility in this store scope to "Not visible individually"

If you don't have flat catalogs enabled and don't need to support this functionality, you can simply use:

$collection = Mage::getModel('catalog/product')->getCollection()
                ->setStoreId($myStoreId)
                ->addAttributeToFilter('visibility', array(
                    'neq' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE
                ));

Or, if you use different root categories for your store, you could simply set a category filter:

$rootCatId = Mage::app()->getStore($myStoreId)->getRootCategoryId();
$rootCat = Mage::getModel('catalog/category')->load($rootCatId);
$collection = Mage::getModel('catalog/product')->getCollection()
                    ->setCategoryFilter($rootCat);

If you use flat catalogs, a detailed explanation of the problem and maybe a solution can be found here: How to set a store ID on Mage_Catalog_Model_Resource_Product_Collection?

1

$storeId = 1;

$collection = Mage::getModel('catalog/product')->getCollection();

$website = Mage::app()->getStore($storeId)->getWebsite();

$collection->addWebsiteFilter($website);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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