Question

Currently in a Category view page, its sub categories are displayed in a list format. I would like to change the list to select option so when there are too many sub categories users does not have to scroll all the way to the bottom to view the content.

Current Code:

<ul>
<?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat): ?>
    <li><a href="<?php echo $_subcat->getUrl() ?>"><?php echo Mage::helper('catalog/output')->categoryAttribute($_subcat, $_subcat->getName(), 'name') ?></a></li>
<?php endforeach ?>
</ul>

How can I accomplish this so the sub-categories is displayed in a select option?

Was it helpful?

Solution

Just replace that code with...

<?php if($this->getCurrentCategory()->getChildrenCount() > 0) { ?>
    <select onchange="document.location = this.options[this.selectedIndex].getAttribute('rel')">
    <?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat): ?>
        <option rel="<?php echo $_subcat->getUrl(); ?>"><?php echo Mage::helper('catalog/output')->categoryAttribute($_subcat, $_subcat->getName(), 'name'); ?></option>
    <?php endforeach ?>
    </select>
<?php } ?>

This will create a drop-down select box of the exact same categories that would have been displayed with the existing code, it will also navigate the user to the category when they click on a selection.

EDIT: This is now TESTED AND WORKING code.

OTHER TIPS

This might help you.

<select id="category" class="myinput-text required-entry widthinput" name="category">
<?php
  $parentid=5; // parent id which you want sub category
  $categories=explode(',',Mage::getModel('catalog/category')->load($parentid)->getChildren());
  foreach($categories as $cat){
  $category=Mage::getModel('catalog/category')->load($cat);
?>
<option value="<?php echo $category->getId();?>"><?php echo $category->getName();?></option>
<?php } ?>
</select>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top