How to get a category listing from Magento?


I want to create a page in Magento that shows a visual representation of the categories.. example

 product 1
 product 2

 product 3

My problem is, their database is organised very differently to what I've seen in the past. They have tables dedicated to data types like varchar, int, etc. I assume this is for performance or similar.

I haven't found a way to use MySQL to query the database and get a list of categories. I'd then like to match these categories to products, to get a listing of products for each category. Unfortunately Magento seems to make this very difficult.

Also I have not found a method that will work from within a page block.. I have created showcase.phtml and put it in the XML layout and it displays and runs its PHP code. I was hoping for something easy like looping through $this->getAllCategories() and then a nested loop inside with something like $category->getChildProducts().

Can anyone help me?

9/25/2015 8:14:06 AM

From code found in an SEO related class (Mage_Catalog_Block_Seo_Sitemap_Category)

$helper     = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories('name', true, false);
$array      = $helper->getStoreCategories('name', false, false);

Try to forget that it's a database that's powering your store, and instead concentrate on using the objects that the Magento system provides.

For example, I had no no idea how to get a list of categories. However, I grepped through the Mage codebase with

grep -i -r -E 'class.+?category'

Which returned a list of around 30 classes. Scrolling through those, it was relatively easy to guess which objects might have methods or need to make method calls that would grab the categories.

7/29/2014 4:43:14 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow