Here is the following code we can use to Create a Custom CSV using some values and Download.
Currently, I am trying to download the csv for all products of Orders. Here is the Sample Code I am using in Controller given below -
$orders = Mage::getModel("sales/order")->getCollection();
// prepare CSV header
$csv = '';
$_columns = array(
"Order Id",
"Product Name",
"Sku",
"Price"
);
$data = array();
// prepare CSV header...
foreach ($_columns as $column) {
$data[] = '"'.$column.'"';
}
$csv .= implode(',', $data)."\n";
foreach ($orders as $order) {
$items = $order->getAllItems();
// echo "Order - ".$order->getId()."<br>";
foreach ($items as $item) {
$loadProduct = Mage::getModel('catalog/product')->load($item->getProductId());
//prepare csv contents
$data = array();
$data[] = $loadProduct['id'];
$data[] = $loadProduct['name'];
$data[] = $loadProduct['sku'];
$data[] = $loadProduct['price'];
//...
$csv .= implode(',', $data)."\n";
//now $csv varaible has csv data as string
}
}
}
$this->_redirect('*/*/');
$this->_prepareDownloadResponse('file.csv', $csv, 'text/csv');
No comments:
Post a Comment