2

这是我的第一个 Magento 项目,我遇到了这么大的问题。我使用的 Magento 版本是 1.7.0.2 我设法使用“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>

它负责生成类别导航。但问题是,每个链接指向的 URL 不起作用。所有导致 404 错误页面的链接都是无效的。你可以在这里看到它:http ://wicked-shop.dev.thejinstudio.com/shop/catalog/category/view/s/accessories/id/15/

我做了很多搜索,但没有什么能真正解决这个问题。我提前感谢您的帮助。

4

1 回答 1

1

使用catalog/category帮助程序从类别模型中获取正确的 URL

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

然后使用该助手将您的类别传递给getCategoryUrl()函数

$_catalogCatgoryHelper->getCategoryUrl($category);

所以试试这个。我已将我的建议放入您的代码中:

<?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>

使用我过去使用的这段代码可能会更好(但实际上最终重写,以便它迭代地显示类别,无论它们有多深,研究如何做到这一点对你来说肯定是一个很好的练习)。它有点干净,但您需要根据需要稍微修改它:

<?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>

编辑:原始代码仅显示当前查看类别的子类别

于 2012-09-21T05:44:14.463 回答