Skip to content

Instantly share code, notes, and snippets.

@bluec
Created November 28, 2016 13:17
Show Gist options
  • Save bluec/b7d64e19ca389094416475794f801563 to your computer and use it in GitHub Desktop.
Save bluec/b7d64e19ca389094416475794f801563 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);
}
@mrmoyeez
Copy link

mrmoyeez commented Dec 3, 2019

This code is for Magento 1 or Magento 2 ??

@bluec
Copy link
Author

bluec commented Dec 3, 2019

It's Magento 1. It was written a long time ago and I'd forgotten all about it so I can't claim for how effective or correct it is!

@moazzama
Copy link

moazzama commented Aug 5, 2021

``<?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);

// $file = fopen('thumbnail.csv', 'r', '"'); // set path to the CSV file
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$storeManager = $objectManager->get('\Magento\Store\Api\StoreRepositoryInterface');
$currentStore = $storeManager->get('de');
print_r($currentStore->getData());
// die();

// 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();

    // Convert the numeric path into category names
    $path = explode('/', $fianl_aaray[$id]['path']);
    $pathnames = array();
    foreach ($path as $pathId)
    {
        // Ignore categories 1 and 2
        if(in_array($pathId, [1])) {
            continue;
        };

        $pathnames[] = $fianl_aaray[$pathId]['name'];
        // print_r($pathnames);
        // die();
    }

print_r($category->getData());

    $header[]=array($id,implode('/', $pathnames),$url_key,$url_path,$imagePath);

// fputcsv($output, array($id, $name, $description, $title, $metaD));

}
$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);
?>

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