2
votes

this is my first Magento project and I've run into such a big issue. The Magento version I'm using is 1.7.0.2 I managed to create a custom left bar navigation bar using the following code in my "app/design/frontend/default/default/template/catalog/navigation/category.phtml"

<?php
$cats = Mage::getModel('catalog/category')->load(1)->getChildren();
$catIds = explode(',',$cats);
?>
<div id="LeftCategory">
    <h2>Wicked Categories</h2>
    <hr style="color:white; height:1px; margin:5px;" />
<ul>
<?php foreach($catIds as $catId): ?>
    <li class="Li-Top-Category">
        <?php
            $category = Mage::getModel('catalog/category')->load($catId);
            ?>
            <a href="<?php echo $category->getUrl()?>">
    <?php echo $category->getName()?>
</a>
<?php

            $subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
            $subCatIds = explode(',',$subCats);
        ?>
            <?php if(count($subCatIds) > 1):?>
                <ul>
                <?php foreach($subCatIds as $subCat) :?>
                    <li class="Li-Sub-Category">
                    <?php
                        $subCategory = Mage::getModel('catalog/category')->load($subCat);
            ?>
            <a href="<?php echo $subCategory->getUrl()?>">
    <?php echo $subCategory->getName()?>
</a>
<?php

                    ?>
                    </li>
                <?php endforeach;?>
                </ul>
            <?php endif; ?>
    </li>
<?php endforeach; ?>
</ul>

</div>

It does the work to generate the category navigation. But the issue is, the URL each link points to doesn't work. All links are invalid that lead to the 404 Error Page. You can see it here: http://wicked-shop.dev.thejinstudio.com/shop/catalog/category/view/s/accessories/id/15/

I've done so much search and nothing really solved this issue. I appreciate your help in advance.

1
Seems you have created all these categories as "Root category". Root category doesn't have the URL key to browse. You need to change these categories to Subcategories of "Root category".Prasath Albert
@PrasathAlbert Thank you for the point. How do I change "Root Category" to "Sub-category"? I don't see an option in Category Manager page.. Again, my version is 1.7.0.2Jin Wang
@PrasathAlbert I made it. Just drag and drop. The system will just work. Thank you!Jin Wang

1 Answers

1
votes

Use the catalog/category helper to get the proper URL from a category model

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

Then with that helper pass your category to the getCategoryUrl() function

$_catalogCatgoryHelper->getCategoryUrl($category);

So give this a try. I've put my suggestions into your code:

<?php
$_catalogCatgoryHelper = Mage::helper('catalog/category');
$cats = Mage::getModel('catalog/category')->load(1)->getChildren();
$catIds = explode(',',$cats);
?>
<div id="LeftCategory">
    <h2>Wicked Categories</h2>
    <hr style="color:white; height:1px; margin:5px;" />
<ul>
<?php foreach($catIds as $catId): ?>
    <li class="Li-Top-Category">
        <?php
            $category = Mage::getModel('catalog/category')->load($catId);
            ?>
            <a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($category) ?>">
    <?php echo $category->getName()?>
</a>
<?php

            $subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
            $subCatIds = explode(',',$subCats);
        ?>
            <?php if(count($subCatIds) > 1):?>
                <ul>
                <?php foreach($subCatIds as $subCat) :?>
                    <li class="Li-Sub-Category">
                    <?php
                        $subCategory = Mage::getModel('catalog/category')->load($subCat);
            ?>
            <a href="<?php echo $_catalogCatgoryHelper->getCategoryUrl($subCategory)?>">
    <?php echo $subCategory->getName()?>
</a>
<?php

                    ?>
                    </li>
                <?php endforeach;?>
                </ul>
            <?php endif; ?>
    </li>
<?php endforeach; ?>
</ul>

</div>

You might be better off with this code which I used in the past (but actually ended up rewriting so that it iteratively displays categories regardless of how deep they go, looking into how to do this would certainly be a good exercise for you). It is a bit cleaner, but you'll need to modify it slightly to your needs:

<?php $helper = $this->helper('catalog/category') ?>
<div class="block block-categorynavigation">
    <div class="block-title">
        <strong><span><?php echo $this->__('Category') ?></span></strong>
    </div>
    <div class="block-content">
        <?php $categories = $this->getStoreCategories() ?>
        <?php if (count($categories) > 0): ?>
            <ul id="leftnav-tree" class="level0">
                <?php foreach($categories as $category): ?>
                    <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
                        <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
                            <?php $subcategories = $category->getChildren() ?>
                            <?php if (count($subcategories) > 0): ?>
                                <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
                                    <?php foreach($subcategories as $subcategory): ?>
                                        <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
                                            <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
                                                <?php $subsubcategories = $subcategory->getChildren() ?>
                                                <?php if (count($subcategories) > 0): ?>
                                                    <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level2">
                                                        <?php foreach($subsubcategories as $subsubcategory): ?>
                                                        <li class="level2<?php if ($this->isCategoryActive($subsubcategory)): ?> active<?php endif; ?>">
                                                            <a href="<?php echo $helper->getCategoryUrl($subsubcategory) ?>"><?php echo $this->escapeHtml(trim($subsubcategory->getName(), '- ')) ?></a>
                                                        </li>
                                                        <?php endforeach; ?>
                                                    </ul>
                                                <?php endif; ?>
                                        </li>
                                    <?php endforeach; ?>
                                </ul>
                            <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>
</div>

edit: Original code only showed subcatgories for the currently viewed category