Magento-addStoreFilter无法正常工作? [英] Magento - addStoreFilter not working?

查看:61
本文介绍了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:

和两个存储具有不同根类别.总店是默认的样本数据,商店2只有一个产品我补充道.我本以为使用商店过滤器,只会显示当前商店根目录类别中的产品.但是我正在展示所有产品.我正在通过在我的类别视图模板中放置以下内容来对此进行测试:

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只有一个产品,那么为什么我会收到来自各门店的每一件产品退换吗?我可以将主要商店"中的每个产品设置为不在"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被包括(如下面所建议的),代码的部分的 {{表}}.STORE_ID = 1 只是设置的所有产品为具有STORE_ID 1.我如何才能获得与该产品相关的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天全站免登陆