简体   繁体   English

根据产品ID列表获取产品集合

[英]Get a collection of products base on list of product id

Given a product id , I can query the product using 给定产品id ,我可以使用查询产品

Mage::getModel('catalog/product')->load($id);

What I have is a list of ids (comma separated), I can explode it, loop through each id, and run load($id) like above. 我所拥有的是ID列表(以逗号分隔),我可以将其explode ,遍历每个ID并像上面那样运行load($id) I am concern a bit about the performance. 我对性能有些担心。 Is this a different way to handle it, something like where clause, with an IN(id1,id2,id3,id4) kind of syntax. 这是一种不同的处理方式,类似于where子句,具有IN(id1,id2,id3,id4)语法。 I google around, and I see this 我在Google周围搜索,看到了这个

Mage::getModel('catalog/product')->getCollection()->addAtributeToSelect('*')

I think I can add a filter to this, right? 我想可以为此添加一个过滤器,对吗? Had anyone solve a similar problem? 有人解决过类似的问题吗? Thank you very much. 非常感谢你。

1) Filter your collection using Product Ids you have : 1)使用您拥有的产品ID过滤您的收藏:

$productIds = explode(',', "1,2,3,4,5,6");
$collection = Mage::getModel('catalog/product')->getCollection()-
>addAttributeToFilter('entity_id', array('in' => $productIds));

2) If you want to retrive only specific information like name & sku etc, you can add attribute to select, this means collection will only fetch the name from database tables, rather than whole product information, you can select with below code 2)如果您只想检索名称和sku等特定信息,则可以添加属性进行选择,这意味着集合将仅从数据库表中获取名称,而不是整个产品信息,您可以使用以下代码进行选择

$collection->addAttributeToSelect(array('name','sku'));

3) Make Sure All this code is written in blocks or models and not in Phtmls, or else it can definitely affect the page speed. 3)确保所有这些代码都是以块或模型而不是Phtml编写的,否则肯定会影响页面速度。

As par r requirement you can use finset function of magento which accepts array as parameter 根据要求,您可以使用magento的finset函数,该函数接受数组作为参数

Try to use addAttributeToFilter with or condition 尝试将addAttributeToFilter与or条件一起使用

$collection->addAttributeToFilter($attribute,
    array(
        array('finset'=> array('237')),
        array('finset'=> array('238')),
        array('finset'=> array('239')),
    )
);
Or

$collection->addAttributeToFilter(
    array(
        array('attribute'=> 'attributecode','finset' => array('237')),
        array('attribute'=> 'attributecode','finset' => array('237')),
        array('attribute'=> 'attributecode','finset' => array('237')),
    )
);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM