Reputation: 38705
Given a product id
, I can query the product using
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. 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. I google around, and I see this
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.
Upvotes: 1
Views: 1813
Reputation: 9055
1) Filter your collection using Product Ids you have :
$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
$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.
Upvotes: 4
Reputation: 970
As par r requirement you can use finset function of magento which accepts array as parameter
Try to use addAttributeToFilter with or condition
$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')),
)
);
Upvotes: 1