Magento-加载根集合将加载所有产品 [英] Magento - Loading root collection loads all products

查看:84
本文介绍了Magento-加载根集合将加载所有产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用具有不同根类别的多个商店:

Regarding using multiple stores, with different root categories:

我建立了2个具有不同根源的商店.一种产品有14种产品,另一种产品有6种.

I have 2 stores set up, with different roots. One has 14 products, the other 6.

如果我在首页上使用以下内容(只是为了显示该商店的根类别中有多少产品-在这种情况下,ID为8),我将获得20种产品-因此该商店中的所有产品根:

If I use the following on my homepage (simply to show how many products are in the root category for that store - in this case with an ID of 8) I get 20 products - so all products in the store, from all roots:

$_testproductCollection = Mage::getModel('catalog/category')->load(8)  
->getProductCollection()  
->addAttributeToSelect('*')->load();
echo "NO. OF PRODUCTS IS ".$_testproductCollection->count();

但是,如果我将ID更改为子类别,则会得到正确数量的产品.该根目录中只有6种产品:

However, if I change the ID to a sub-category, I get the correct amount of products. There are only 6 products in this root:

但是计数显示为20(因为整个商店中有20个-或都有两个根).

But the count shows 20 (as there's 20 in the whole store - or both roots).

有人知道怎么回事吗?是虫子吗?

Anyone know what's up? Is it a bug?

我还注意到,如果您转到管理产品"并使用商店视图过滤器,它什么也不会做,仍然会在商店视图中显示20种产品,而其根目录只有6种产品:

I also noticed that if you go to Manage Products and use the store view filter, it doesn't do anything, still showing 20 products in the store view whose root has only 6 products:

推荐答案

好的,我认为这可行,并没有进行太多测试,但似乎已经完成了.您需要首先获取商店的根类别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-加载根集合将加载所有产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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