Created
March 18, 2014 17:26
-
-
Save tegansnyder/9624873 to your computer and use it in GitHub Desktop.
Export Categorys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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