Question

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

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

Was it helpful?

Solution

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.

OTHER TIPS

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));
} 

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>|

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

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.

Yes, sure. You can use a third-party tool called Skyvia for this purpose. It connects to Magento via its API and allows exporting all major Magento tables in CSV files. It has a free account, too, so you can test it. Saves a lot of time as requires little to no configuration and coding

Another way of doing it would be to go to:

Magento2 > Catalog > Categories...

... then click on 'Expand All' and copy and paste the full list of categories into a word or excel document.

This was the quickest way of getting a list of all existing categories for me... some further formatting in excel or word was required, but it did the job.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top