In a multistore setup, I need to find products that are assigned to website A and website B.
The product collection has a addWebsiteFilter()
method but unfortunately it doesn't serve this purpose, as can be seen in Mage_Catalog_Model_Resource_Product_Collection::_productLimitationJoinWebsite()
, where the filter gets applied:
$conditions[] = $this->getConnection()
->quoteInto('product_website.website_id IN(?)', $filters['website_ids']);
The query will match for all products that are assigned to any of the given websites. What is the best way to build a query for products that are present in multiple websites?
Best Answer
There it is. Given
$websiteIds
is an array of website ids:I removed
DISTINCT
and grouped by product instead to be able toCOUNT
the number of joined websites. If it is equal to the total number of filtered websites, the product is assigned to all of them.