Magento - 加载根集合加载所有产品

发布于 2024-10-24 08:53:10 字数 710 浏览 1 评论 0原文

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

我设置了 2 个具有不同根的商店。一个有 14 个产品,另一个有 6 个。

如果我在主页上使用以下内容(只是为了显示该商店的根类别中有多少产品 - 在本例中 ID 为 8),我会得到 20 个产品 - 所以所有产品在商店中,从所有根源:

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

但是,如果我将 ID 更改为子类别,我会获得正确数量的产品。此根目录中只有 6 个产品:

roots

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

有人知道怎么回事吗?这是一个错误吗?

我还注意到,如果您转到“管理产品”并使用商店视图过滤器,它不会执行任何操作,仍然在商店视图中显示 20 个产品,其根目录只有 6 个产品

: sstatic.net/NrFwS.jpg" alt="过滤器">

Regarding using multiple stores, with different root categories:

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

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();

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:

roots

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?

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:

filter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2024-10-31 08:53:10

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

$_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/>"; 
};

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/>"; 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文