How to get product collection which is assigned to ONLY ONE STORE? This is not working by passing store ID to ->addStoreFilter();
Does have any idea?
If you want to select a product that is in a specific store and only in that specific store (not in the other stores), then this requires a bit more than only doing some filtering on the product collection.
First let me point out that products are connected to a website through their store, so the actual question is how to get a product from a website that is not in other websites...
Here's the code that should do the trick:
// 1. Get the store (current store or change the code to get
// any other store or website)
$store = Mage::app()->getStore();
// 2. We need to get the IDs of the other websites
$otherWebsiteIds = Mage::getModel('core/website')->getCollection()
->addFieldToFilter('website_id', array('neq' => $store->getWebsiteId()))
->getAllIds();
// 3. Now we will build a query that is getting the product ID
// from the catalog_product_website table where it is in the
// other websites
$pw = Mage::getResourceModel('catalog/product_website');
$pwSubSelect = $pw->getReadConnection()->select()->from(['pw' => $pw->getMainTable()], 'product_id')
->where('pw.product_id = e.entity_id')->where('pw.website_id IN(?)', $otherWebsiteIds);
// 4. Now we're getting to the product collection...
// Add your own filters here (for instance certain product IDs or categories)...
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addStoreFilter($store->getId());
// 5. Now let's say the product_id should not have any result in the
// sub query of number 3. (so not be in the other websites)
$collection->getSelect()->where(
'e.entity_id NOT IN(?)',
new Zend_Db_Expr((string)$pwSubSelect)
);
This could result in an SQL query looking like this (here I have 3 websites and the current website has ID 3
):
SELECT `e`.* FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_website` AS `product_website`
ON product_website.product_id = e.entity_id AND product_website.website_id = '3'
WHERE (e.entity_id NOT IN(
SELECT `pw`.`product_id` FROM `catalog_product_website` AS `pw`
WHERE (pw.product_id = e.entity_id) AND (pw.website_id IN('0', '1', '2'))
))
7ochems approach looks good, but here is another one ... don't know which performs better ...
$yourId = 5; # has to match websites default store ID
$storeIds = array();
foreach (Mage::app()->getWebsites() as $website) {
$storeIds[] = $website->getDefaultStore()->getId();
}
$uniqueIds = array();
$dupeIds = array();
foreach ($storeIds as $storeId) {
$products = Mage::getModel('catalog/product')
->getCollection()
->addStoreFilter($storeId);
if ($yourId == $storeId) {
$uniqueIds = $products->getAllIds();
} else {
$dupeIds = array_unique(array_merge($dupeIds, $products->getAllIds()));
}
}
$uniqueIds = array_diff($uniqueIds, $dupeIds);
// final collection
$products = Mage::getModel('catalog/product')
->getCollection()
->addIdFilter($uniqueIds);
$dupeIds
array could become 720k items big. So if you only have a few products (maybe a few thousand), this is just fine...
array_unique()
upwards, so max size is fixed by overall product count. 300k should not take sooo much memory. (?)
Try this code:
$collection = Mage::getResourceModel('catalog/product_collection')->addStoreFilter(Mage::app()->getStore('store_id'));
Try to use this code :
$collection = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('store_id','enter store_id number');
$collection = Mage::getModel('catalog/product')->getCollection()
->setStoreId('your store id');
Try this code :
$storeId=Mage::app()->getStore()->getStoreId();
$collection= Mage::getModel('catalog/product')->getCollection()->setStoreId($storeId);