Created
November 28, 2016 13:17
-
-
Save bluec/b7d64e19ca389094416475794f801563 to your computer and use it in GitHub Desktop.
Export Magento category tree with full names, paths and URLs
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 | |
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
``<?php
// header('Content-Type: text/csv; charset=utf-8');
// header('Content-Disposition: attachment; filename=report.csv');
// Load the Magento core
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0);
set_time_limit(0);
error_reporting(E_ALL);
// Load the category collection
// $categories = Mage::getModel('catalog/category')
// ->getCollection()
// ->addAttributeToSelect('')
// ->addIsActiveFilter();
$objectManager = $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()->addAttributeToSelect('')->addIsActiveFilter()->setStore($currentStore);
$header[]=array('id','name','url_key','url_path','image');
$fianl_aaray = array();
foreach ($categories as $ids => $category) {
$id = $category->getId();
$name = $category->getName();
$url_key = $category->getData('url_key');
$url_path = $category->getData('url_path');
$imagePath = $category->getData('magepow_thumbnail');
// Ignore categories 1
if(in_array($id, [1])) {
continue;
};
$fianl_aaray[$id]['name'] = $category->getName();
$fianl_aaray[$id]['path'] = $category->getPath();
}
$fp = fopen("getCatPath.csv","w"); //first the read the file and header and create the data in csv
// $fp = fopen('php://output', 'wb');
foreach ($header as $line) {
fputcsv($fp, $line);
}
fclose($fp);
?>