How to get Magento categories with full paths

Maybe someone will need it, so here we go with code:

 

the snippet above will give you such output:

root category 1 ; 1
root category 1 > subcategory 1 ; 2
root category 1 > subcategory 1 > subsubcategory 1 ; 3
root category 1 > subcategory 1 > subsubcategory 2 ; 4
root category 1 > subcategory 2 ; 5
root category 1 > subcategory 2 > subsubcategory 1 ; 6

9 comments

  1. \n didn’t worked for me, so I used PHP_EOL. Also I added some code from another php-Script to save the category-tree in a .csv file (path=var/export/catwithid.csv):

     

  2. Thanks for neat snippet.  For folks who wants to fetch all categories (not just current store categories), make sure your script is executed in admin store. Something like the code below will help. When script is ran as admin-store categories are read from ‘catalog_category_entity’  table. Without admin, categories are fetched from catalog_category_flat_store_x table.

Leave a Reply