How can I remove html tags while exporting order.csv from magento admin panel. ?
3 Answers
The previous solution would not be the best way because when exporting in xml format, the TAGS are also removed. Here's another solution:
app/code/core/Mage/Adminhtml/Block/Widget/Grid.php
There is a attribute called $_isExport that's protected,so you have to create a method public in your class, returning this attribute, in these case the class:
app/code/community/MyNameSpace/MyModule/Block/Adminhtml/Sales/Order/Grid.php like this:
public function isExport()
{
return $this->_isExport;
}
I created a Render to display on the grid.
app/code/community/MyNameSpace/MyModule/Block/Adminhtml/Sales/Order/Grid/Renderer/Item/Option.php
like this:
<?php
class MyNameSpace_MyModule_Block_Adminhtml_Sales_Order_Grid_Renderer_Item_Option extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row) {
$data = unserialize($row->getProductOptions());
$result = array();
if ($this->getColumn()->getGrid()->isExport()) {
$result = 'CLIENTE: ' . $data['additional_options'][4]['value'] . ' - '
. 'PLIB: ' . $data['additional_options_verified'][4]['value'];
} else {
$style = ($data['additional_options'][4]['value'] != $data['additional_options_verified'][4]['value'])
? "style = 'color:#F00'" : "style = 'color:#000'";
$result[] = '<b>CLIENTE:</b> ' . $data['additional_options'][4]['value'];
$result[] = '<b>PLIB:</b> ' . '<b '.$style.'>' . $data['additional_options_verified'][4]['value'];
$result = implode('<br />',$result);
}
return $result;
}
}
As you can see, with few lines and without rewrite/override Magento Classes
I hope it helped you!
You can escape HTML tags by overriding the _prepareDownloadResponse()
controller function. This function declares headers and content file in response for file download.
To do so, you have to first rewrite/override the Mage_Adminhtml_Sales_OrderController
controller class same as below.
app/code/local/Namespace/Module/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
...
<admin>
<routers>
<adminhtml>
<args>
<modules>
<Namespace_Module before="Mage_Adminhtml">Namespace_Module_Adminhtml</Namespace_Module>
</modules>
</args>
</adminhtml>
</routers>
</admin>
...
</config>
app/code/local/Namespace/Module/controllers/Adminhtml/Sales/OrderController.php
<?php
require_once "Mage/Adminhtml/controllers/Sales/OrderController.php";
class Namespace_Module_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
{
/**
* Declare headers and content file in response for file download
*
* @param string $fileName
* @param string|array $content set to null to avoid starting output, $contentLength should be set explicitly in
* that case
* @param string $contentType
* @param int $contentLength explicit content length, if strlen($content) isn't applicable
* @return Mage_Core_Controller_Varien_Action
*/
protected function _prepareDownloadResponse(
$fileName,
$content,
$contentType = 'application/octet-stream',
$contentLength = null)
{
...
if (!is_null($content)) {
if ($isFile) {
...
// strip tags from data
while ($buffer = strip_tags($ioAdapter->streamRead())) {
print $buffer;
}
...
} else {
$this->getResponse()->setBody($content);
}
}
return $this;
}
}
as you can see, strip_tags
is being used to strip the HTML tags before assigning into the buffer variable.
Hope it will help.
strip_tags() will be used to strip the HTML tags on export. Just define following function inside your controller :
protected function _prepareDownloadResponse(
$fileName,
$content,
$contentType = 'application/octet-stream',
$contentLength = null)
{
$session = Mage::getSingleton('admin/session');
if ($session->isFirstPageAfterLogin()) {
$this->_redirect($session->getUser()->getStartupPageUrl());
return $this;
}
$isFile = false;
$file = null;
if (is_array($content)) {
if (!isset($content['type']) || !isset($content['value'])) {
return $this;
}
if ($content['type'] == 'filename') {
clearstatcache();
$isFile = true;
$file = $content['value'];
$contentLength = filesize($file);
}
}
$this->getResponse()
->setHttpResponseCode(200)
->setHeader('Pragma', 'public', true)
->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
->setHeader('Content-type', $contentType, true)
->setHeader('Content-Length', is_null($contentLength) ? strlen($content) : $contentLength, true)
->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"', true)
->setHeader('Last-Modified', date('r'), true);
if (!is_null($content)) {
if ($isFile) {
$this->getResponse()->clearBody();
$this->getResponse()->sendHeaders();
$ioAdapter = new Varien_Io_File();
$ioAdapter->open(array('path' => $ioAdapter->dirname($file)));
$ioAdapter->streamOpen($file, 'r');
//Strip HTML here
while ($buffer = strip_tags($ioAdapter->streamRead())) {
print $buffer;
}
$ioAdapter->streamClose();
if (!empty($content['rm'])) {
$ioAdapter->rm($file);
}
exit(0);
} else {
$this->getResponse()->setBody($content);
}
}
return $this;
}