How to create import functionality on grid page in magneto
Open Grid.php
protected function _prepareMassaction()
{
..................
..................
//Upload your csv file into var/import directory
$dir = new DirectoryIterator(Mage::getBaseDir() . DS . 'var' . DS . 'import' . DS);
$extensions = "csv";
$csv = array();
foreach ($dir as $fileinfo) {
if ($fileinfo->isFile() && stristr($extensions, $fileinfo->getExtension())) {
$csv[$fileinfo->getFilename()] = $fileinfo->getFilename();
}
}
$csv = array_merge(array(''=>'Please select'),$csv);
$this->getMassactionBlock()->addItem('import', array(
'label' => Mage::helper('metizsoft_taxgenerate')->__('Import'),
'url' => $this->getUrl('*/*/massCsv', array('_current'=>true)),
'confirm' => Mage::helper('metizsoft_taxgenerate')->__('Are you sure?'),
'additional' => array(
'subgroup' => array(
'name' => 'csv',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('customer')->__('Select Csv'),
'values' => $csv
)
)
));
return $this;
}
Open your controller file. and put this code.public function massCsvAction()
{
$csvfile = $this->getRequest()->getParam('csv');
if (!$csvfile) {
Mage::getSingleton('adminhtml/session')->addError(
Mage::helper('metizsoft_taxgenerate')->__('Please select statetaxs.')
);
} else {
try {
$csv = new Varien_File_Csv;
$csvpath = Mage::getBaseDir() . DS . 'var' . DS . 'import' . DS . $csvfile;
$datas = $csv->getData($csvpath);
print_r($datas);
} catch (Mage_Core_Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError(
Mage::helper('metizsoft_taxgenerate')->__('There was an error updating statetaxs.')
);
Mage::logException($e);
}
}
$this->_redirect('*/*/index');
}
No comments:
Post a Comment