Get a list of all magento categories and their id on screen when i visit php file

I want to create a PHP file that will return a list of all product categories from magento and their id when I visit that file. For example, when I visit mysite.com/category.php it should show me a list of all categories and their respective IDs. I searched stackoverflow and every other forum out there, I found many similar questions but nothing solves my problem. I have no problem with the code itself, but I am struggling to figure out where to put this code, here is the code I want to use.

category = Mage::getModel('catalog/category');
$catTree = $category->getTreeModel()->load();
$catIds = $catTree->getCollection()->getAllIds();
$cats = array();
if ($catIds){
    foreach ($catIds as $id){
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        $cats[$cat->getId()] = $cat->getName();
    }
}

      

Can someone please explain what file I need to put this code inside and also could you explain in which folder I need to put the file inside and also if I need to change the code to get the desired result. Many thanks

EDIT

This is what I have done so far. so I create a PHP file called category.php. I put this file in the root of my magento folder, which will be the folder with the app, skins and media directories inside, then I included my code in the file like

$category = Mage::getModel('catalog/category');
$catTree = $category->getTreeModel()->load();
$catIds = $catTree->getCollection()->getAllIds();
$cats = array();
    if ($catIds){
        foreach ($catIds as $id){
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        $cats[$cat->getId()] = $cat->getName();
    }
}

      

I have now included the code that @mufaddal has kindly included in his answer

$mageFilename = 'app/Mage.php';

require_once $mageFilename;

      

So my complete category.php file should look like this

<?php
$mageFilename = 'app/Mage.php';

require_once $mageFilename; 

$category = Mage::getModel('catalog/category');
$catTree = $category->getTreeModel()->load();
$catIds = $catTree->getCollection()->getAllIds();
$cats = array();
    if ($catIds){
        foreach ($catIds as $id){
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        $cats[$cat->getId()] = $cat->getName();
    }
}
?>

      

Once I saved this file, I opened my web browser and typed the url of my mysite.com/category.php file and then I should see a list of all categories on the screen, but all I get is a blank screen may can anyone explain if i am really stupid and something is wrong.

+3


source to share


2 answers


You should just print the variables. You can do something like this:

<?php $mageFilename = 'app/Mage.php';

require_once $mageFilename;

Mage::init();

$category = Mage::getModel('catalog/category');
$catTree = $category->getTreeModel()->load();
$catIds = $catTree->getCollection()->getAllIds();
$cats = array();
    if ($catIds){
        foreach ($catIds as $id){
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        $cats[$cat->getId()] = $cat->getName();
    } 
} 

// Optionally you can use <pre> tag for a neater print
echo "<pre>";
var_dump($cats);
echo "</pre>";

      

Here is some sample code on how you can get categories and their children at different levels. You can change level 2 to whatever level you want, but usually the 2 categories are the main categories below the root category, so that should be the way to go!



<?php

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::init();

$category = Mage::getModel('catalog/category')->getCollection()
                    ->addAttributeToFilter('level',2);

$catIds = $category->getAllIds();

$cats = array();
$i=0;

if ($catIds){
    foreach ($catIds as $id){
        $j=0;
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        $cats[$i]["Category ID:"] = $id;
        $cats[$i]["Category Name:"] = $cat->getName();
        $subcats = $cat->getChildren();
        foreach(explode(',',$subcats) as $subCatid){
            $subcat = Mage::getModel('catalog/category');
            $subcat->load($subCatid);
            $cats[$i][$j]["Subcategory ID:"] = $subCatid;
            $cats[$i][$j]["Subcategory Name:"] = $subcat->getName();
            $j++;
        }
        $i++;
    }
} 

foreach($cats as $row){
    echo "<pre>";
    print_r($row);
    echo "</pre>";
}

      

As ugly as it sounds, you can get the next levels of subcategories using foreach () for each level. For example, this gets subcategories:

if ($catIds){
  foreach ($catIds as $id){
    $j=0;
    $cat = Mage::getModel('catalog/category');
    $cat->load($id);
    $cats[$i]["Category ID:"] = $id;
    $cats[$i]["Category Name:"] = $cat->getName();
    $subcats = $cat->getChildren();
    foreach(explode(',',$subcats) as $subCatid){
      $subcat = Mage::getModel('catalog/category');
      $subcat->load($subCatid);
      $cats[$i][$j]["Subcategory ID:"] = $subCatid;
      $cats[$i][$j]["Subcategory Name:"] = $subcat->getName();
      $_subcats = $subcat->getChildren();
      foreach(explode(',',$_subcats) as $_subCatid){
        $_subcat = Mage::getModel('catalog/category');
        $_subcat->load($_subCatid);
        $cats[$i][$j][$k]["Sub-subcategory ID:"] = $_subCatid;
        $cats[$i][$j][$k]["Sub-subcategory Name:"] = $_subcat->getName();
        $k++;
      }
      $j++;
    }
    $i++;
  }
} 

      

+2


source


Just put the file in the root of your magento folder and include the Mage.php file to work with magento. how



$mageFilename = 'app/Mage.php';

require_once $mageFilename;

      

0


source







All Articles