I try to export particular order information in csv format but i can't able to download. What mistake i done here?
observer.php
public function adminhtmlWidgetContainerHtmlBefore($event)
{
$block = $event->getBlock();
$message = Mage::helper('sales')->__('Are you want to Download...?');
if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
$block->addButton('download', array(
'label' => Mage::helper('Sales')->__('Download...'),
'onclick' => "confirmSetLocation('{$message}','{$block->getUrl('*/')}')", // < missed comma
'class' => 'go'
));
}
}
controller.php
public function downloadAction(){
$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();
foreach ($items as $item) {
$loadProduct = Mage::getModel('catalog/product')->load($item->getProductId());
//prepare csv contents
$data = array();
$data[] = $order->getId();
$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');
}
config.xml
<?xml version="1.0"?>
<config>
<modules>
<Manoj_Csv>
<version>0.1.0</version>
</Manoj_Csv>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_view>Manoj_Csv_Block_Adminhtml_Sales_Order_View</sales_order_view>
</rewrite>
</adminhtml>
</blocks>
</global>
<adminhtml>
<events>
<adminhtml_widget_container_html_before>
<observers>
<manoj_csv>
<class>Manoj_Csv_Model_Observer</class>
<method>adminhtmlWidgetContainerHtmlBefore</method>
</manoj_csv>
</observers>
</adminhtml_widget_container_html_before>
</events>
</adminhtml>
</config>
Mage::log('yes it is called', null, 'myfile.log');inside the function and check if file name calledmyfile.logis in/var/log, if no file then function is not called. – Adarsh Khatri Apr 04 '16 at 05:14