Magento - addStoreFilter 不起作用? [英] Magento - addStoreFilter not working?

查看:23
本文介绍了Magento - addStoreFilter 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Magento 中获取产品集合时,我希望 StoreFilter 做到这一点,按当前商店进行过滤.但我无法让它工作.

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.

假设我有 2 个像这样设置的商店:

Say I have 2 stores set up like so:

而且两家商店都有不同的根类别.Main Store 是默认的示例数据,Store2 只添加了一个产品.我原以为使用商店过滤器,只会显示当前商店的根类别内的产品.但我正在展示所有产品.我正在通过在我的类别视图模板中放置以下内容来对此进行测试:

And both stores have a different root category. Main Store is the default sample data, Store2 has just one product I added. I would have thought that using the store filter, only products within the root category of the current store would show up. But I'm getting every product showing. I'm testing this by placing the following in my category view template:

$store_id = Mage::app()->getStore()->getId();
$_testproductCollection = Mage::getResourceModel('reports/product_collection')
->setStoreId($storeId)
->addStoreFilter($store_id)
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){ 
echo $this->htmlEscape($_testproduct->getName()); 
};

如果我回显商店 ID,它会给我正确的号码.我在商店 2 中只有一种产品,那么为什么我从所有商店都退回了所有产品?我可以将 Main Store 中的每个产品设置为不在 Store2 中显示,然后添加一个可见性过滤器,但这需要永远.

If I echo the store ID, it's giving me the correct number. I have only one product in Store 2, so why am I getting every product from all stores returned? I can set every product in Main Store to not show in Store2 and then add a visibility filter, but that would take forever.

另外,我刚刚注意到,如果我回显产品商店 ID,我会得到当前 ID,而不是分配给它的商店:

Also, I just noticed, if I echo the products store ID, I get the current ID, not the store it's assigned to:

echo $_testproduct->getStoreId()

怎么回事?

更新(2011 年 4 月 8 日):好的,所以我尝试加入字段以便包含 store_id(如下所示),代码部分 {{table}}.store_id = 1 只是将所有产品设置为具有 store_id1. 我怎样才能获得与产品关联的 store_id?

UPDATE (April 8 2011): OK, so I tried joining the fields so that the store_id is included (as suggested below), the section of code {{table}}.store_id = 1 is just setting all the products to have a store_id of 1. How can I just get the store_id associated with the product?

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

foreach($_testproductCollection as $_testproduct){  
echo $this->htmlEscape($_testproduct->getName())."<br/>"; 
echo "STORE IS ".$_testproduct->getData('store_id')."<br/>";
};

如果我检查数据库的 catalog_category_product_index 表,store_id 是正确的.

If I check the catalog_category_product_index table of my db, the store_id's are correct.

推荐答案

好的,我认为这有效,还没有测试太多,但似乎已经成功了.您需要首先获取您的商店根类别 ID,然后加入一些字段以便您可以访问产品category_id",然后使用它进行过滤:

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:

$_rootcatID = Mage::app()->getStore()->getRootCategoryId();

$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $_rootcatID))
->addAttributeToSelect('*');
$_testproductCollection->load();

foreach($_testproductCollection as $_testproduct){ 
    echo $this->htmlEscape($_testproduct->getName())."<br/>"; 
};

这篇关于Magento - addStoreFilter 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆