Thursday, June 20, 2013

Magento : Create CSV file for any custom data in magento

It can be done by using Varien_File_Csv class
Exp:

$file_path = '/your_dir_path/sample.csv';
$mage_csv = new Varien_File_Csv(); //mage CSV 
$products_ids = array(1,2,3); //product ids or make array of any data
$products_model = Mage::getModel('catalog/product');
$products_row = array();
foreach ($products_ids as $pid)
{
   $prod = $products_model->load($pid);
   $data = array();
   $data['sku'] = $prod->getSku();
   $data['name'] = $prod->getName();
   $data['price'] = $prod->getPrice();    
   $products_row[] = $data;              
}
$mage_csv->saveData($file_path, $products_row);

2 comments:

  1. Best tips to lean how to create CSV files. Thanks for sharing this. Am still a newbie on magento so this blog is helpful for me.

    ReplyDelete
  2. Hi! Could you please suggest how to export a csv of invoices & order data

    ReplyDelete