2

Used this script for ages but now need the actual category Ids to be exported.

    public function getAttributes() {
    $data = array();
    $data[] = array (‘field’ => ‘id’, ‘label’ => ‘ID’,’required’ => ‘yes’);
    $data[] = array ('field' => 'name', 'label' => 'Name','required' => 'yes');
    $data[] = array ('field' => 'path', 'label' => 'Path', 'function' => 'getFullPath','required' => 'yes' );
    $data[] = array ('field' => 'position', 'label' => 'Position');
    $data[] = array ('field' => 'is_active', 'label' => 'Is Active', 'function' => 'getYesNo');
    $data[] = array ('field' => 'url_key', 'label' => 'Url Key');
    $data[] = array ('field' => 'description', 'label' => 'Description');
    $data[] = array ('field' => 'image', 'label' => 'Image');
    $data[] = array ('field' => 'meta_title', 'label' => 'Page Title');
    $data[] = array ('field' => 'meta_keywords', 'label' => 'Meta Keywords');
    $data[] = array ('field' => 'meta_description', 'label' => 'Meta Description');
    $data[] = array ('field' => 'include_in_menu', 'label' => 'Include In Menu', 'function' => 'getYesNo');
    $data[] = array ('field' => 'display_mode', 'label' => 'Display Mode', 'function' => 'getDisplayMode');
    $data[] = array ('field' => 'landing_page', 'label' => 'CMS Block', 'function' => 'getStaticBlock');
    $data[] = array ('field' => 'is_anchor', 'label' => 'Is Anchor', 'function' => 'getYesNo');
    $data[] = array ('field' => 'available_sort_by', 'label' => 'Availabe Sort By', 'function' => 'getProductSortBy');
    $data[] = array ('field' => 'default_sort_by', 'label' => 'Default Sort By', 'function' => 'getProductSortBy');
    $data[] = array ('field' => 'page_layout', 'label' => 'Page Layout', 'function' => 'getPageLayout');
    $data[] = array ('field' => 'custom_layout_update', 'label' => 'Custom Layout Update');
    return $data;
}}

this being the new line added

$data[] = array (‘field’ => ‘id’, ‘label’ => ‘ID’,’required’ => ‘yes’);

but it doesnt export the category id numbers.

  • replace id with entity_id. – Marius Feb 11 at 11:27
1

Simple script to export categories Ids in CSV

<?php
 define('MAGENTO_ROOT', getcwd());
 $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
 require_once $mageFilename;
 Mage::app();
 $allCategories = Mage::getModel ( 'catalog/category' );
 $categoryTree = $allCategories->getTreeModel();
 $categoryTree->load();
 $categoryIds = $categoryTree->getCollection()->getAllIds(); 

 if ($categoryIds) {
     $outputFile = "var/importexport/categories-and-ids.csv";
     $write = fopen($outputFile, 'w');

 foreach ( $categoryIds as $categoryId ) {
         $data = array($allCategories->load($categoryId)->getName(), $categoryId);
         fputcsv($write, $data);
     }
 }

 fclose($write);
 ?>

I hope this will help

  • Hi Just found that it works perfectly. Thanks Was looking at now activating the categories en mass. Starting a new post – always magento issues Feb 11 at 11:34
  • would love to but reputation is too low – always magento issues Feb 11 at 13:07
2

You have issue in your syntax, replace "‘" to "'" and id with entity_id so please replace your first line with following

$data[] = array ('field' => 'entity_id', 'label' => 'ID','required' => 'yes'); 
1

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.

    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.