【问题标题】:Magento sort Categories in templateMagento 对模板中的类别进行排序
【发布时间】:2012-03-07 09:57:16
【问题描述】:

我正在寻找一种方法来对导航中类别的前端显示进行排序。

这是我的导航代码:

<div id="menu-accordion" class="accordion">      
    <?php 

    foreach ($this->getStoreCategories() as $_category): ?>
    <?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
    <h3 class="accordion-toggle"><a href="#"><?php print $_category->getName();?></a></h3>
        <div class="accordion-content">
                <ul>
                <?php foreach ($_category->getChildren() as $child): ?>
                    <li> 
                        <span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
                            <a href="<?php print $this->getCategoryUrl($child); ?>"><?php print $child->getName();?></a>
                    </li>
                <?php endforeach; ?>
                </ul>
            </div>
    <?php endforeach ?>
</div>

我尝试使用asort()$this-&gt;getStoreCategories() 进行排序,但它解决了错误500,所以我猜它不是一个数组,而是一个对象(这对于magento 的面向对象编程来说似乎很明显)。我试图找到对象的解决方案,但失败了,现在我有点卡住了。

感谢您的帮助。

【问题讨论】:

    标签: php arrays sorting magento object


    【解决方案1】:

    $this-&gt;getStoreCategories() 的调用不返回数组。但是您可以构建自己的数组,并使用数组的键作为要排序的元素(假设您要按类别名称排序):

    foreach ($this->getStoreCategories() as $_category)
    {
        $_categories[$_category->getName()] = $_category;
    }
    
    ksort($_categories);
    

    现在不是遍历$this-&gt;getStoreCategories(),而是遍历$_categories 数组。所以你的代码看起来像:

    <div id="menu-accordion" class="accordion">      
        <?php 
    
        $_categories = array();
        foreach ($this->getStoreCategories() as $_category)
        {
            $_categories[$_category->getName()] = $_category;
        }
        ksort($_categories);
    
        foreach ($_categories as $_category): ?>
        <?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
        <h3 class="accordion-toggle"><a href="#"><?php print $_category->getName();?></a></h3>
            <div class="accordion-content">
                    <ul>
                    <?php foreach ($_category->getChildren() as $child): ?>
                        <li> 
                            <span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
                                <a href="<?php print $this->getCategoryUrl($child); ?>"><?php print $child->getName();?></a>
                        </li>
                    <?php endforeach; ?>
                    </ul>
                </div>
        <?php endforeach ?>
    </div>
    

    【讨论】:

    • 好的,主要类别的效果很好,但子类别没有排序。
    • 我对子类别做了同样的事情,现在它几乎完美了,我只需要找到一个缓存方法。谢谢
    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    相关资源
    最近更新 更多