1

I want to load a set of products, given as an array:

<?php
$productSkus = array('sku-1','sku-2');

foreach($productSkus as $productSku) :
    $productId = Mage::getModel("catalog/product")->getIdBySku($productSku);
    $_product = Mage::getModel('catalog/product')->load($productId);
    $productName = $_product->getName();
    echo $productName;
endforeach;
?>

As I am a rookie I'm wondering if this set is not a lot of database requests. Is there a way to do the loading before the loop at once?

2 Answers 2

1

Use addAttributeToFilter

$productSkus = array('sku-1','sku-2');
$productCollection=Mage::getModel('catalog/product')->getCollection()
       ->addAttributeToFilter('sku', array('in' => $productSkus))
       ->addAttributeToSelect('*');
foreach($productCollection as $_product)
{
     echo $_product->getName();
}
2
  • I get a Parse error: syntax error, unexpected '*' in Jun 17, 2015 at 10:09
  • that was a type-error. i missed the single quotes. check the updated answer Jun 17, 2015 at 10:12
1

In this case you can use product collection and filter them by sku using in clause.

    $Collection=Mage::getResourceModel('catalog/product_collection')
      -->addAttributeToFilter('sku', array(
        'in' => $productSkus,
        ))
     ->addAttributeToSelect('name') // for get Name;
;



foreach($Collection as $eachproduct) :
echo $eachproduct>getName();
endforeach;

For getting more details on filter see here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.