I’ve been trying for months now to filter a product collection by it’s store root category. I’ve tried numerous different ways ( Magento – addStoreFilter not working? , Magento ->addCategoryFilter – filtering product collection by root category , Magento – Loading root collection loads all products ) but still haven’t found anything that works.
So I want to try adding in the store_id to the products in a collection. I can call the collection using the following, but I need to join some fields in order to add the store_id:
$_testproductCollection = Mage::getResourceModel('catalog/product_collection');
$_testproductCollection->getSelect()->distinct(true);
$_testproductCollection->addAttributeToSelect('*')->load();
The joinField I need is something like the following (which was given in a post linked above) but the store_id=1 part changes every products store_id to 1, which I don’t want, I just want a way to add it’s designated store so I can then filter by it.
$_testproductCollection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
Can anyone point me in the right direction? Cheers!
OK, I think this works, haven’t tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products “category_id”, then filter using that: