rss
twitter
    Find out what I'm doing, Follow Me :)

Tuesday, November 10, 2009

Pin It

Widgets

Tips For Creating Dynamic Category Landing Pages

This will  give you a starting point for building a static block and PHTML file that can be applied to top level categories to dynamically create a block with of all the subcategories.

First create a new static block (CMS → Static Blocks), lets call it ‘Dynamic Landing Pages’. Within the content paste this:
{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}} 

If you’ve not seen one of these before, then basically it loads the list.phtml into the block allowing you to include dynamic content in your site.

Next create list.phtml in app/design/frontend/default/your_theme/template/catalog/category/

Then go to ‘Manage Categories’ select the relevant category and then choose ‘Static Block Only’ in the ‘Display Mode’ dropdown and then choose the ‘Dynamic Landing Pages’ static block we’ve just created from the ‘CMS Block’ dropdown.

Now to look at some coding to put in list.phtml, to get the category id of the current category and an array containing the ids of its child categories, use the following:

$current = $this->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load((int)$current);
$children = $category->getChildren();
$children = explode(",",$children);
 
This leaves you with:
$current, your current category id.
$category, which is an instance of Mage_Catalog_Model_Category.
$children, is an array of the child category ids.

Then cycle through each of the child categories by loading the category object using the id from the array. We check first that the first value is not just an empty string (as it will be if there are no categories):

if (strlen($children[0]) > 0)
{
 foreach($children as $child)
 {
  $_child = Mage::getModel('catalog/category')->load($child);
   // then use the $_child object to pull out category properties
 }
}

Some of the key methods that subsequently used to create the landing pages were:
  • $_child->getName()
  • $_child->getUrl()
  • $_child->getImageUrl()
  • $_child->getProductCount()
The Image URL is based on the image that is uploaded in the ‘General Information’ tab in ‘Manage Categories’ but be aware that if you use this functionality you will probably want to edit
frontend/default/your_theme/template/catalog/category/view.phtml so that it doesn’t load that image at the top of the subcategory.

To view the methods of the Mage_Catalog_Model_Category class look
at the file: app/code/core/Mage/Catalog/Model/Category.php.
 

2 comments:

  1. by doing this we still not have a subcategories in sort order by its position

    ReplyDelete
  2. This is awesome post regarding create dynamic category in magento.thanks for sharing with this post.I very thankful to you .

    ReplyDelete