{"id":211,"date":"2018-10-19T12:02:56","date_gmt":"2018-10-19T12:02:56","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2018\/10\/19\/import-csv-in-magento-configuration\/"},"modified":"2025-05-22T17:16:45","modified_gmt":"2025-05-22T11:46:45","slug":"import-csv-in-magento-configuration","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/import-csv-in-magento-configuration\/","title":{"rendered":"How to Import CSV in Magento Configuration"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">CSV is a file format for easy data import-export, notably helpful in Magento to add products in bulk, migrate products, reviews import-export, inventories, customer data to transfer a large number of data between two programs, manage catalogs, customer data management, and much more.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Working with CSV files in Magento is very common. Developers often need to<i>&nbsp;<\/i><strong>import CSV in Magento configuration<\/strong>, and read and extract its data. Similar to importing CSV in Magento configuration you can also&nbsp;<a title=\"How to Add Date and Time Picker in Magento Configuration\" href=\"https:\/\/meetanshi.com\/blog\/add-date-and-time-picker-in-magento-configuration\/\">add date and time picker in Magento configuration<\/a>&nbsp;to provide admin with some settings related to it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we know how important CSV files are, let\u2019s go ahead with the method to&nbsp;<i>import CSV in Magento configuration<\/i>. Follow the below code for successful implementation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Import CSV in Magento Configuration:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the below code in the <strong>system.xml<\/strong>&nbsp;file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;import translate=\"label\">\n    &lt;label>Import  CSV&lt;\/label>\n    &lt;frontend_type>import&lt;\/frontend_type>\n    &lt;backend_model>extension\/upload&lt;\/backend_model>\n    &lt;sort_order>50&lt;\/sort_order>\n    &lt;show_in_default>1&lt;\/show_in_default>\n    &lt;show_in_website>1&lt;\/show_in_website>\n    &lt;show_in_store>1&lt;\/show_in_store>\n&lt;\/import><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>Upload.php<\/strong>&nbsp;file at&nbsp;<strong>app\/code\/local\/Vendor\/Extension\/Model<\/strong>&nbsp;directory<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Vendor_Extension_Model_Upload extends Mage_Core_Model_Config_Data\n{\n    public function _afterSave()\n    {\n        Mage::getModel('extension\/import')->uploadAndImport();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>Import.php<\/strong>&nbsp;file at&nbsp;<strong>app\/code\/local\/Vendor\/Extension\/Model<\/strong>&nbsp;directory<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Vendor_Extension_Model_Import extends Mage_Core_Model_Abstract\n{\n    public function uploadAndImport()\n    {\n        if (empty($_FILES['groups']['tmp_name']['extension']['fields']['import']['value'])) {\n            return $this;\n        }\n        $csvFile = $_FILES['groups']['tmp_name']['extension']['fields']['import']['value'];\n        $io = new Varien_Io_File();\n        $info = pathinfo($csvFile);\n        $io->open(array('path' => $info['dirname']));\n        $io->streamOpen($info['basename'], 'r');\n        $headers = $io->streamReadCsv();\n        if ($headers === false || count($headers) &lt; 1) {\n            $io->streamClose();\n            Mage::throwException(Mage::helper('extension')->__('Invalid CSV File'));\n        }\n        try {\n            $count = 0;\n            while (false !== ($csvLine = $io->streamReadCsv())) {\n                if (empty($csvLine) || $csvLine[0] == '')\n                    continue;\n                    \n                    Mage::log($csvLine[0]);\n                $count++;\n            }\n            Mage::getSingleton('core\/session')->addSuccess(\n                Mage::helper('extension')->__('Successfully imported ' . $count . ' record')\n            );\n        } catch (Mage_Core_Exception $e) {\n            Mage::throwException($e->getMessage());\n        } \n        return $this;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run the above code and easily read or extract data from the CSV files!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSV is a file format for easy data import-export, notably helpful in Magento to add products in bulk, migrate products, reviews import-export, inventories, customer data&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":15576,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/211\/revisions\/15576"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}