Basic Magento Functions to get Category Information


Load category by id

<?php

      $categoryid =89;

      $_category = Mage::getModel('catalog/category')
                   ->load($categoryid);
      //get url of loaded category
      $_category_url = $_category->getUrl();
?>

Get the current category/product/cms page

<?php

      $currentCategory = Mage::registry('current_category');

      $currentProduct = Mage::registry('current_product');

      $currentCmsPage = Mage::registry('cms_page');

?>

Get The Root Category In Magento

<?php

      $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();

      $_category = Mage::getModel('catalog/category')->load($rootCategoryId);

      // You can then get all of the top level categories using:

      $_subcategories = $_category->getChildrenCategories();

?>

Load Products by Category ID

<?php

      $_category = Mage::getModel('catalog/category')->load(47);

      $_productCollection = $_category->getProductCollection();

      if($_productCollection->count()) {

          foreach( $_productCollection as $_product ):

                  echo $_product->getProductUrl();

                  echo $this->getPriceHtml($_product, true);

                  echo $this->htmlEscape($_product->getName());

          endforeach;

       }

?>
Advertisements

3 thoughts on “Basic Magento Functions to get Category Information

  1. Pingback: Add Custom “Sort By” Drop Down menu options In Magento | Magento For Fresher

  2. Pingback: Magento Product and Category Related Functions | Magento For Fresher

  3. Pingback: All Catogory Listing in Magento | Magento For Fresher

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