7

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

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

16

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.

  • Thanks for this. I had to use addslashes (php.net/manual/en/function.addslashes.php) around my category names. – Andy Triggs Aug 18 '15 at 15:49
  • how can i import, need help.. – matinict Aug 21 '16 at 5:00
  • @matinict : You can use magmi, its best for products import – Gem Mar 18 '17 at 4:53
  • @ seanbreeden : Like this way any script for export all products – Gem Mar 18 '17 at 4:56
  • how can i export sku and category path, not category name – user51361 May 24 '17 at 10:03
1

Wasn't keen on @seanbreeden answer because it wont work properly if your exporting the category description and they have HTML or returns in any of the categories' fields like description etc.

Here's my solution (which you visit in the browser and it downloads it):

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=report.csv');

// Load the Magento core
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);

// Load the category collection
$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addIsActiveFilter();

// create a file pointer connected to the output stream
// can change this so it downloads it to a file on the server
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('id', 'name', 'description', 'title', 'meta_description'));

foreach ($categories as $category) {
    $id = $category->getId();
    $name = $category->getName();
    $description = $category->getDescription();
    $title = $category->getMetaTitle();
    $metaD = $category->getMetaDescription();

    fputcsv($output, array($id, $name, $description, $title, $metaD));
} 
0

Matt,In magento you can find of lot of extesion which are export /import category in csv.Using this you can fill your requirement

In my point of view best in free extension in this purpose if:

Magentoworks.net

PaiD:

MagentoCommerce

commerceextensions

More extension in MagentoConnection here

0

Even though you accepted an answer , I think the easiet method is to export all products to CSV and then use excel to distinct the categories field.

Using code or extension if its for a once in a while export , is waste of time and is another class you need to support in the future.

0

You can use Varien_File_Csv::saveData($file, $data). I guess its easier then using fopen, fwrite, ... directly. Also you dont have to take care of permissions, when creating a new export directory. To add fields, you just have to just change $columns variable.

This will save your CSV to var/export/...

require_once('app/Mage.php');
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

/** @var array $columns matches atttribute code*/
$columns = array(
    'entity_id',
    'path',
    'name',
    'description',
);

$data = array(
    $columns,
);

$categories = Mage::getResourceModel('catalog/category_collection')
    ->addAttributeToSelect($columns);
    //->addIsActiveFilter();

foreach ($categories as $category) {
    $row = array();
    foreach ($columns as $column) {
        $row[] = $category->getData($column);
    }
    $data[] = $row;
}

$path = Mage::getBaseDir('var') . DS . 'export' . DS . 'your_directory';
$file = 'categories.csv';

// create directory under var/export
$io = new Varien_Io_File();
$io->checkAndCreateFolder($path);

$csv = new Varien_File_Csv();
$csv->setEnclosure('|'); # change enclosure to work with html
$csv->saveData($path . DS. $file, $data);

Out ...

|entity_id|,|path|,|name|,|description|
|1|,|1|,|Root Catalog|,||
|2|,|1/2|,|Default Category|,||
|4|,|1/2/4|,|Dogs|,|<span class="empty"><span>|
-1

First let me apologize for not having a fancy code to show off programming skills I dont have.

This is how i did it, simply made a product called "0000allcatagories" and just made that product (via lots of clicks) a member of all my catagories. A simple export to a CSV and viola, a list of all the catagories..... now if only i can figure out how to do the same with attributes B)

i found a program called "catagory upload for magento" ... which does a very nice job of downloading catagories... and if you can edit a spreadsheet you can upload catagories as well.... very nice.

good luck

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.