All Catogory Listing in Magento

Some time we wants to display all categories in homepage or
any CMS pages in Magento. There are many different ways to
get the category listing. I will show you how you can get
list of all categories of your Magento Shop.

<strong>1. Display all the Magento Categories(Active/Inactive)</strong>

The following code will fetch all categories (both active
 and inactive) that are presented in your Magento Store.

$categories = Mage::getModel('catalog/category')
              ->getCollection()->addAttributeToSelect('*');

<strong>2. Display All Active Categories</strong>

The following code will fetch all active categories that
are present in your Magento Store. Filtering the inactive categories.

$categories = Mage::getModel('catalog/category')
             ->getCollection()->addAttributeToSelect('*')->addIsActiveFilter();

<strong>3. Display Active Categories Of Any Particular Level in Magento</strong>

The following code will fetch all active categories of certain/specific
 level. Here, I have chosen level 1. Also sorting categories by name.

$categories = Mage::getModel('catalog/category')
              ->getCollection()
              ->addAttributeToSelect('*')
              ->addIsActiveFilter()
              ->addLevelFilter(1)
              ->addOrderField('name');

<strong>4. Display Store Specific Categories</strong>

The following code will fetch all active store specific categories.

getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)

$helper = Mage::helper('catalog/category');

// sorted by name, fetched as collection
$categoriesCollection = $helper->getStoreCategories('name', true, false);

// sorted by name, fetched as array
$categoriesArray = $helper->getStoreCategories('name', false, false);

<strong>5. Display Top level categories only in Magento</strong>

The following code will fetch all the Top level categories only

<?php
      $_helper = Mage::helper('catalog/category')
      $_categories = $_helper->getStoreCategories()
      if (count($_categories) > 0):
 ?>
<ul>
    <?php foreach($_categories as $_category): ?>
      <li>
         <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
         <?php echo $_category->getName() ?>
        </a>
     </li>
   <?php endforeach; ?>
</ul>
<?php endif; ?>

<strong>6. Display Top Level Categories as well as ALL Subcategories in Magento</strong>

The following code will fetch all the Top Level Categories as
well as ALL Subcategories

<?php
      $_helper = Mage::helper('catalog/category')
      $_categories = $_helper->getStoreCategories()
      $currentCategory = Mage::registry('current_category')
      if (count($_categories) > 0):
?>
<ul>
   <?php foreach($_categories as $_category): ?>
     <li>
        <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
          <?php echo $_category->getName() ?> //Top Level Category Listing
        </a>
        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
        <?php $_subcategories = $_category->getChildrenCategories() ?>
        <?php if (count($_subcategories) > 0): ?>
        <ul>
            <?php foreach($_subcategories as $_subcategory): ?>
            <li>
              <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> //Sub Category Listing
                 <?php echo $_subcategory->getName() ?>
              </a>
            </li>
           <?php endforeach; ?>
       </ul>
       <?php endif; ?>
    </li>
  <?php endforeach; ?>
</ul>
<?php endif; ?>

<strong>7. Display Subcategories only for the Current Top Category</strong>

The following code will fetch all Subcategories only for the
Current Top Category

<?php $_currentCategory = Mage::register('current_category') ?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>" title="<?php echo $_category->getName() ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($_category->getId() == $_currentCategory->getId()): ?>
<?php $_subcategories = $_currentCategories->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>" title="<?php echo $_subcategory->getName() ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?></pre>
<h6 class="zemanta-related-title" style="font-size: 1em;">Related articles</h6>
<ul class="zemanta-article-ul">
	<li class="zemanta-article-ul-li"><a href="http://singhnarendra434.wordpress.com/2013/07/29/display-categories-and-subcategories-in-magento/" target="_blank">Display Categories and SubCategories in Magento</a> (singhnarendra434.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="http://singhnarendra434.wordpress.com/2013/07/24/show-all-category-on-magento-sidebar/" target="_blank">Show all category on magento sidebar</a> (singhnarendra434.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="http://singhnarendra434.wordpress.com/2013/07/24/add-magento-category-links-to-your-footer-template/" target="_blank">Add Magento Category Links to your Footer Template</a> (singhnarendra434.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="https://magentofresher.wordpress.com/2013/07/27/add-custom-sort-by-drop-down-menu-options-in-magento/" target="_blank">Add Custom "Sort By" Drop Down menu options In Magento</a> (magentofresher.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="https://magentofresher.wordpress.com/2013/07/31/magento-image-switcher-on-mouseover-more-view-product-page/" target="_blank">Magento More View Image Switcher On MouseOver Product Page</a> (magentofresher.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="https://magentofresher.wordpress.com/2013/07/26/basic-magento-functions-to-get-category-information/" target="_blank">Basic Magento Functions to get Category Information</a> (magentofresher.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="http://onlinemagento.wordpress.com/2013/07/25/magento-code-book/" target="_blank">Magento Code Book</a> (onlinemagento.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="https://magentofresher.wordpress.com/2013/07/26/basic-magento-functions-to-get-cart-information/" target="_blank">Basic Magento Functions to get Cart Information</a> (magentofresher.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="https://magentofresher.wordpress.com/2013/07/31/magento-product-and-category-related-functions/" target="_blank">Magento Product and Category Related Functions</a> (magentofresher.wordpress.com)</li>
	<li class="zemanta-article-ul-li"><a href="https://magentofresher.wordpress.com/2013/07/30/magento-frequently-used-fuctions/" target="_blank">Magento Frequently used fuctions</a> (magentofresher.wordpress.com)</li>
</ul>
<pre>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s