Search This Blog

Wednesday, May 25, 2011

magento custom csv import module

The basic case you may wonder how to do, is import of data for your custom module. Let's do this by example. Imagine you need to display on you e-shop list of stores, you have created custom module, table in database and datamodel part, all you need now is to populate this table with data you have within csv file.

Read the file
First you have to read the file. As you already know (if you read Magento Dataflow - Default Adapters [Part 2]) you can use dataflow/convert_adapter_io adapter for this.


file
var/import





Parse the file content

Now that you have read the file content, you should parse it using dataflow/convert_parser_csv.





true

1



Process rows of data

Now the custom part of this process. Within your custom module you have to create custom adapter that will create row in database for each processed row of parsed file. Within your module root directory create file ./Model/Convert/Adapter/Store.php of this content:

class Baobaz_Offer_Model_Convert_Adapter_Offer
extends Mage_Dataflow_Model_Convert_Adapter_Abstract
{
protected $_storeModel;

public function load() {
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}

public function save() {
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}

public function getStoreModel()
{
if (is_null($this->_storeModel)) {
$storeModel = Mage::getModel('baobaz_store/store');
$this->_storeModel = Mage::objects()->save($storeModel);
}
return Mage::objects()->load($this->_storeModel);
}

public function saveRow(array $importData)
{
$store = $this->getStoreModel();

if (empty($importData['code'])) {
$message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'code');
Mage::throwException($message);
}
else
{
$store->load($importData['code'],'code');
}

$store->setCode($importData['code']);
$store->setName($importData['name']);

$store->save();

return true;

}
}
Now when you have this file created you can modify a little bit the declaration of parser adding adapter and method variables:





true

1

baobaz_store/convert_adapter_store
saveRow


Having this done you should have your xml definition of custom dataflow profile looking like that:


file
var/import






true

1

baobaz_store/convert_adapter_store
saveRow


Source http://blog.baobaz.com/en/blog/customizing-magento-dataflow-import-of-custom-data
http://www.ayasoftware.com/content/how-import-configurable-products-csv-file-magento-system

No comments:

Post a Comment