Friday 8 July 2011

How to export Categories with Ids in Magento?

Using the following given quick PHP script you can export categories with Ids in Magento.You just need to save te below given code in a PHP file “CatList.php” in the base Magento directory of your store, and visit the URL in your web browser.

<?php
 
 define('MAGENTO', realpath(dirname(__FILE__)));
 require_once MAGENTO . '/app/Mage.php';
 Mage::app();
 
 $category = Mage::getModel ( 'catalog/category' );
 $tree = $category->getTreeModel ();
 $tree->load ();
 
 $ids = $tree->getCollection ()->getAllIds ();
 
 if ($ids) {
  $file = "var/import/catlist.csv";
  file_put_contents($file,"catId, catName\n");
  foreach ( $ids as $id ) {
    $string = $id . ', ' .$category->load($id)->getName() . "\n";
   file_put_contents($file,$string,FILE_APPEND);
  }
 }
?>

You can run this script as http://www.domain.com/CatList.php. You can find the csv file in the var/import/ directory.

3 comments:

  1. thanks man
    nice code

    ReplyDelete
  2. Hi
    Can we have a script to import this csv file in magento

    ReplyDelete
  3. This works great! Thanks for posting this up, beats doing it by hand any day of the week.

    ReplyDelete