Skip to content

Instantly share code, notes, and snippets.

@mrmoyeez
Forked from bluec/export_categories.php
Created December 3, 2019 11:48
Show Gist options
  • Save mrmoyeez/a6f8cecc497df4d49bcd2605cf847284 to your computer and use it in GitHub Desktop.
Save mrmoyeez/a6f8cecc497df4d49bcd2605cf847284 to your computer and use it in GitHub Desktop.
Export Magento category tree with full names, paths and URLs
<?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();
$categories = array();
if ($ids)
{
// Open file and print headers
$fp = fopen('category_export.csv', 'w');
$headers = ['id', 'name', 'path', 'url'];
fputcsv($fp, $headers, ",", '"');
// Loop throught every category ID in the category tree
foreach ($ids as $id)
{
// Ignore categories 1 and 2
if(in_array($id, [1,2])) {
continue;
};
// Load the category and get the URL key, name, and path
$category->load($id);
$url_key = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(), true);
$categories[$id]['name'] = $category->getName();
$categories[$id]['path'] = $category->getPath();
// Convert the numeric path into category names
$path = explode('/', $categories[$id]['path']);
$pathnames = array();
foreach ($path as $pathId)
{
// Ignore categories 1 and 2
if(in_array($pathId, [1,2])) {
continue;
};
//$pathCat=Mage::getModel('catalog/category')->load($pathId);
//$pathnames[]=$pathCat->getName();
$pathnames[] = $categories[$pathId]['name'];
}
// Save this line to the CSV file
$line = [$id, $category->getName(), implode(' > ', $pathnames), $url_key];
fputcsv($fp, $line, ",", '"');
}
// Close the file handle
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment