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> 
8
  • What error do you get? Commented Apr 4, 2016 at 5:00
  • No error No download
    – Magento 2
    Commented Apr 4, 2016 at 5:10
  • Can you confirm function is called. Commented Apr 4, 2016 at 5:11
  • I think so How to check
    – Magento 2
    Commented Apr 4, 2016 at 5:12
  • 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. Commented Apr 4, 2016 at 5:14

1 Answer 1

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.

19
  • No luck No download
    – Magento 2
    Commented Apr 4, 2016 at 11:06
  • I can confirm it worked on my side. Commented Apr 4, 2016 at 11:07
  • Can you please update the config.xml i think i made a mistake there
    – Magento 2
    Commented Apr 4, 2016 at 11:08
  • Download Csv button not apper my code Download.. Button only show in that page
    – Magento 2
    Commented Apr 4, 2016 at 11:13
  • On which page? Button should appear in order view page. Commented Apr 4, 2016 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.