Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created March 18, 2014 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegansnyder/9624873 to your computer and use it in GitHub Desktop.
Save tegansnyder/9624873 to your computer and use it in GitHub Desktop.
Export Categorys
<?php
require_once 'app/Mage.php';
Mage::app();
$x = 0;
$categories = array();
$csv = array();
$category = Mage::getModel ('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
if ($ids) {
// populate full paths
foreach ($ids as $id) {
$category->load($id);
$categories[$id]['name'] = $category->getName();
$categories[$id]['path'] = $category->getPath();
}
// then loop through the categories again
foreach ($ids as $id) {
$category->load($id);
// EE 1.13 uses getUrlKey instead of getUrlPath magic method of category object
$url_key = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlKey());
$path = explode('/', $categories[$id]['path']);
$fpath = '';
foreach ($path as $pathId) {
$fpath .= $categories[$pathId]['name'] . '/';
}
$path_ids = implode(',', $path);
$csv[$x]['category_id'] = $id;
$csv[$x]['category_name'] = $category->getName();
$csv[$x]['category_full_path'] = $fpath;
$csv[$x]['category_path_ids'] = $path_ids;
$csv[$x]['url_key'] = $url_key;
$x = $x + 1;
}
$fp = fopen('exported-cats.csv', 'a');
$headerDisplayed = false;
foreach ($csv as $val) {
if (!$headerDisplayed) {
fputcsv($fp, array_keys($val));
$headerDisplayed = true;
}
fputcsv($fp, $val);
}
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment