Magento-1 CSV Import Export – How to Export All Categories into a CSV

csvexportimportexportmagento-1

Is there a way to export all categories into a csv?

Im moving data from a non-magento store into a fresh build.

Best Answer

You could write this one pretty easily. Create a PHP file called something like export.php.

<?php

require("app/Mage.php");  // load the main Mage file
Mage::app();   // not run() because you just want to load Magento, not run it.

// load all of the active categories in the system and include all attributes
$categories = Mage::getModel('catalog/category')
              ->getCollection()
              ->addAttributeToSelect('*')
              ->addIsActiveFilter();

 $export_file = "var/export/categories.csv"; // assumes that you're running from the web root. var/ is typically writable
 $export = fopen($export_file, 'w') or die("Permissions error."); // open the file for writing.  if you see the error then check the folder permissions.

 $output = "";

 $output = "id,name\r\n"; // column names. end with a newline.
 fwrite($export, $output); // write the file header with the column names


 foreach ($categories as $category) {
     $output = ""; // re-initialize $output on each iteration
     $output .= $category->getId().','; // no quote - integer
     $output .= '"'.$category->getName().'",'; // quotes - string
     // add any other fields you want here 
     $output .= "\r\n"; // add end of line
     fwrite($export, $output); // write to the file handle "$export" with the string "$output".
 }

 fclose($export); // close the file handle.

You could write an import in a similar way, but use Mage::getModel('catalog/category') with magic sets to fill in the fields to create new categories while parsing the file you've created.

Related Topic