更改Magento子类别的排序顺序 [英] Change sort order of Magento subcategories

查看:122
本文介绍了更改Magento子类别的排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上显示与当前类别关联的所有子类别的列表.下面的代码可以正常工作,但是我想更改子类别列表的排序方式.当前,它按类别ID排序.我希望它以Magento用户在管理员中放置类别的任何顺序显示(他们可以拖放以更改类别顺序).感谢任何帮助!

I am working on a site where I am displaying a list of all the subcategories associated with the current category. The code below works fine for that, but I'd like to change the way the list of subcategories is sorted. Currently, it sorts by category ID. I'd like it to show up in whatever order the Magento user put the categories in in the admin (where they can drag-and-drop to change the category order). Appreciate any help!

             <?php
                $currentCat = Mage::registry('current_category');

                if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
                {
                    // current category is a toplevel category
                    $loadCategory = $currentCat;
                }
                else
                {
                    // current category is a sub-(or subsub-, etc...)category of a toplevel category
                    // load the parent category of the current category
                    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
                }
                $subCategories = explode(',', $loadCategory->getChildren());

                foreach ( $subCategories as $subCategoryId )
                {
                    $cat = Mage::getModel('catalog/category')->load($subCategoryId);

                    if($cat->getIsActive())
                    {
                        echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>';
                    }
                }
            ?>

推荐答案

尝试调用getChildrenCategories,这将考虑每个类别的位置:

Try calling getChildrenCategories this will take into account the position of each category:

$loadCategory->getChildrenCategories()

已编辑

与getChildren不同的是,getChildren返回所有类别ID的字符串,它会返回Mage_Catalog_Model_Category数组,因此需要更改您的代码以考虑到这一点.

Unlike getChildren which returns a string of all category ids returns an array of Mage_Catalog_Model_Category so your code will need to be changed to take this into account.

在上面的代码段中,以下更改应起作用.请注意对getChildrenCategories()的调用以及foreach循环中的更改,因为每个项目都应该是类别对象.

From your code snippet above the following change should work. Note the call to getChildrenCategories() and the change in the foreach loop as each item should be a category object.

<?php
$currentCat = Mage::registry('current_category');

if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
{
    // current category is a toplevel category
    $loadCategory = $currentCat;
}
else
{
    // current category is a sub-(or subsub-, etc...)category of a toplevel category
    // load the parent category of the current category
    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
$subCategories = $loadCategory->getChildrenCategories();

foreach ( $subCategories as $subCategory )
{
    if($subCategory->getIsActive())
    {
        echo '<a href="'.$subCategory->getURL().'">'.$subCategory->getName().'</a>';
    }
}
?> 

这篇关于更改Magento子类别的排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆