Pages

Thursday, August 1, 2013

Script to export magento categories

If you are after a quick method to get all the category ID’s for your categories for some Excel lookups (for new data imports), then this quick PHP script should help you out:

<?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/catwithid.csv";
        file_put_contents($file,"catId, catNamen");
        foreach ( $ids as $id ) {
          $string = $id . ', ' .$category->load($id)->getName() . "n";
            file_put_contents($file,$string,FILE_APPEND);
        }
    }
?>

No comments:

Post a Comment