Quick script to export Magento categories with IDs

If you are after a quick method to get all the category ID’s for your categories for some Excel lookups (for new data imports), then this quick PHP script should help you out:

<?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 ();
	
	if ($ids) {
		$file = "var/import/catwithid.csv";
		file_put_contents($file,"catId, catNamen");
		foreach ( $ids as $id ) {
		  $string = $id . ', ' .$category->load($id)->getName() . "n";
			file_put_contents($file,$string,FILE_APPEND);
		}
	}

Simply save the above code in a PHP file in the base Magento directory of your store, and visit the URL in your web browser, simples!

  • dezire

    Hi everybody,

    I need Help!

    how i do this? i need export all the products in stock and I need the following information

    I’d like to export the whole name of the category, not just the numbers of them

    Name;Url;Sku;Manufacturer;Ean;categories

    How i do that export?

    Thanks!

  • Wise

    Hi Ben, i put it the file in to root folder, then i went to see it but i see a blank page? plz can you tel me what im doing wrong?
    I put it also in a sub folder includes but still i can not see the anything.

    thx

  • https://www.sonassi.com Benjamin

    I would suggest checking your PHP error log.

  • https://www.sonassi.com Benjamin

    Do you need to do it regularly or just one-off? If the latter, just use the DataFlow – its slow, but will work without any scripts/code changes.

  • MeoMap

    ( ! ) Fatal error: Uncaught exception ‘Exception’ with message ‘Warning: file_put_contents(var/import/cats.csv) [function.file-put-contents]: failed to open stream: No such file or directory in E:\xampp\htdocs\tigopromos\tpromos\home\tpromos\public_html\var\test.php on line 14′ in E:\xampp\htdocs\tigopromos\tpromos\home\tpromos\public_html\app\code\core\Mage\Core\functions.php on line 245
    ( ! ) Exception: Warning: file_put_contents(var/import/cats.csv) [function.file-put-contents]: failed to open stream: No such file or directory in
    File test.php in var, and this is my error, please help me to resolve it :(

    E:\xampp\htdocs\tigopromos\tpromos\home\tpromos\public_html\var\test.php on line 14 in E:\xampp\htdocs\tigopromos\tpromos\home\tpromos\public_html\app\code\core\Mage\Core\functions.php on line 245
    Call Stack
    # Time Memory Function Location
    1 0.0476 344608 {main}( ) ..\test.php:0
    2 2.9793 8389416 file_put_contents ( ) ..\test.php:14
    3 2.9796 8390272 mageCoreErrorHandler( ) ..\functions.php:0

  • http://www.enigma-webdesign.nl Stevan

    Ok, I ran this script and it works.

    How can I import the file.
    I made an export in Magento 1.4.1 and trying to import in 1.5

    Br,

    Steve

  • http://open-ecommerce.org Eduardo

    thanks a lot Benjamin and kamidan,

    I was geting crazy with the subcategories

    have a beautifull day,

    eduardo

  • sivakumar

    i have three store in magento ,when i run this script i got categories from one store only, remaining store categories are not exported.

  • http://www.facebook.com/placento Sebastijan Placento

    this didn’t work for me… It was placed in root folder of Magento dir.
    Switched first line to: define(‘MAGENTO’, realpath(__DIR__));
    (get the current directory – Added in PHP 5.3.0 ) and it worked like a charm!
    THANK YOU VERY MUCH!

  • Biju

    hi Benjamin, thanks for the script. does it work on 1.7.2? i just tried but returns server error “The website encountered an error while retrieving http://staging.tp.com/categoryids.php. It may be down for maintenance or configured incorrectly.” any idea?

  • Biju

    Hi Benjamin, sorry. it was my stupid mistake of not putting the closing php tag as i had just copied and pasted your code from above! thanks a lot. its done. brilliant work!