Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Last active December 3, 2020 15:31
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tegansnyder/8227501 to your computer and use it in GitHub Desktop.
Save tegansnyder/8227501 to your computer and use it in GitHub Desktop.
Export Category Tree and Paths for Magento
<?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();
$x = 0;
if ($ids) {
$file = "var/import/catwithid.csv";
file_put_contents($file,'"id","name","path","path_ids","url"' . PHP_EOL);
foreach ( $ids as $id ) {
$url_key = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(), true);
$path = explode('/', $categories[$id]['path']);
$fpath = '';
foreach ($path as $pathId) {
$fpath .= $categories[$pathId]['name'] . '/';
}
$path_ids = implode(',', $path);
$string = '"' . $id . '","' . $category->load($id)->getName() . '","' . $fpath . '","' . $path_ids . '"' . $url_key . '"' . PHP_EOL;
file_put_contents($file,$string,FILE_APPEND);
}
}
@mrmoyeez
Copy link

mrmoyeez commented Dec 3, 2019

This code if for Magento 1 or Magento 2 ??

@alfredogama
Copy link

just for magento 1.9.x

@itg-dave
Copy link

itg-dave commented Sep 4, 2020

you need to put a comma between $path_ids and $url_key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment