【问题标题】:Change sort order of Magento subcategories更改 Magento 子类别的排序顺序
【发布时间】:2013-02-15 16:17:45
【问题描述】:

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

             <?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>';
                    }
                }
            ?>

【问题讨论】:

    标签: magento


    【解决方案1】:

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

    $loadCategory->getChildrenCategories()
    

    已编辑

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

    从上面的代码 sn-p 中,以下更改应该有效。请注意对 getChildrenCategories() 的调用以及 foreach 循环中的更改,因为每个项目都应该是一个类别对象。

    <?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>';
        }
    }
    ?> 
    

    【讨论】:

    • 该函数是递归的,因为它加载了由 Children() 过滤的集合。请注意,第一个函数 getChildren 返回一个包含所有孩子 id 的字符串,而 getChildrenCategories 返回一个 Mage_Catalog_Model_Category 数组
    • 感谢您的反馈,我已使用此信息更新了我的答案。
    • 不客气。请注意,主要变化是:不再需要 explode() 和 load()。如此简洁的代码。
    • 谢谢大家的回答!您介意发布更新代码 sn-p 吗?我是 Magento 新手,不太确定我还需要什么,不需要什么。 :)
    • 已更新,希望对您有所帮助。请注意对 getChildrenCategories() 的调用和 foreach 循环中的更改,因为每个项目都应该是一个类别对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2013-12-30
    相关资源
    最近更新 更多