Magento - addStoreFilter not working?

前端 未结 4 2108
灰色年华
灰色年华 2020-12-18 09:10

When getting a product collection in Magento, I would expect the StoreFilter to do just that, filter by the current store. But I can\'t get it to work.

Say I have 2

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 09:20

    $_testproductCollection should look like this $_testproductCollection = Mage::getResourceModel('reports/product_collection')->addAttributeToSelect('*')->addStoreFilter().

    If You print SELECT from that collection You will see that there ain't any store column, so addStoreFilter() can't apply WHERE.

    You should use joinField() on Your collection and add store_id column from catalog_product_entity_varchar table.

    EDIT

    Sorry to keep You waiting ;)

    $collection = Mage::getResourceModel('catalog/product_collection');
    $collection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
    $collection->getSelect()->distinct(true);
    

    This should do the trick, but just to be sure, please check if you're getting right products :)

提交回复
热议问题