1

I have been given a list of about 150 SKUs in a CSV and I need to get their product names.

I currently have the following code:

 require_once('app/Mage.php');
 Mage::app();

 $csv = file('Product - Sheet1.csv');
 $data= array();
 foreach($csv as $line){
     $data[] = str_getcsv($line);
 }

 $products= array();
 foreach($data as $k => $entry){
     if ($k < 1) continue; // First row contains the CSV headers
     $products[$k]['sku'] = $entry[0];
     $model == Mage::getSingleton('catalog/product')->loadByAttribute('sku',$entry[0]);
     $products[$k]['title']= $model->getName();
 }

If I do

 print_r($products)

I get a blank page, presumably because I'm loading the catalog/product model in the foreach loop.

How do I achieve the same solution but without overloading the system? I know loading models in a foreach is bad practice but I thought I could get by because its not on a production environment.

1

do one thing.read the csv and try to load all your 150 product skus in an array and use the following code to get the product names

$productSku = array('sku1','sku2');
$productModel = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name')->addAttributeToFilter('sku', array('in' => $productSku));
$productNames = $productModel->getColumnValues('name');
print_r($productNames);

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.