0

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> 
Magento 2
  • 3,834
  • 7
  • 52
  • 107

1 Answers1

1

It's most likely your controller function is not called.

To test: place this Mage::log('yes it is called', null, 'myfile.log'); inside the function and check if file name called myfile.log is in /var/log, if no file then function is not called.

See this line 'onclick' => "confirmSetLocation('{$message}','{$block->getUrl('*/')}'). You might want to check what $block->getUrl('*/') will return?

My approach:

You should rewrite your order view page to add your button there and then can link to your controller.

In config.xml

<?xml version="1.0"?>
<config>
 <global>
    <blocks>         
        <adminhtml>
            <rewrite>
                <sales_order_view>Manoj_Csv_Block_Adminhtml_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>
 </global>
 <admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <csv before="Mage_Adminhtml">Manoj_Csv_Adminhtml</csv>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>
</config> 

Then your Manoj/Csv/Block/Adminhtml/Sales/Order/View.php file

<?php

class Manoj_Csv_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View
{

    public function __construct()
    {

        parent::__construct();

        $order = $this->getOrder();
        $message = Mage::helper('sales')->__('Are you sure you want to download the file?');
                $this->addButton('download_csv', array(
                    'label'     => Mage::helper('sales')->__('Download Csv'),
                    'onclick'   => "confirmSetLocation('{$message}', '{$this->getDownloadCsv($order->getId())}')", 
                ));



    }

    public function getDownloadCsv($id)
    {
        return $this->getUrl('adminhtml/order/download', array('id' => $id));
    }

Now, your Manoj/Csv/controller/Adminhtml/OrderController.php

<?php

class Manoj_Csv_Adminhtml_OrderController extends Mage_Adminhtml_Controller_Action
{

public function downloadAction(){
  $orderId = $this->getRequest()->getParam('id');

  $order = Mage::getModel("sales/order")->load($orderId);
// 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";
         $items = $order->getAllItems();
         foreach ($items as $item) {
               $loadProduct = Mage::getModel('catalog/product')->load($item->getProductId());
               //prepare csv contents
                $data[] = $order->getId();
                $data[] = $loadProduct->getName();
                $data[] = $loadProduct->getSku();
                $data[] = $loadProduct->getFinalPrice();

                $csv .= implode(',', $data)."\n";
               //now $csv varaible has csv data as string
    }

try {
        //download
        $this->_prepareDownloadResponse('file-'.$orderId.'.csv', $csv, 'text/csv');
        $this->_getSession()->addSuccess($this->__('File downloaded.'));
    } catch (Mage_Core_Exception $e) {
        $response = array(
            'error'     => true,
            'message'   => $e->getMessage(),
        );
    }catch (Exception $e) {
        $this->_getSession()->addError(Mage::helper('sales')->__('File cannot be donwloaded.'));
        $response = array(
            'error'     => true,
            'message'   => $this->__('File cannot be downloaded.')
        );
    }
}

I haven't tested but this should work. Good luck.

Adarsh Khatri
  • 8,360
  • 2
  • 25
  • 58