根据管理中的位置对 Magento 中的类别进行排序

发布于 2024-10-30 21:54:57 字数 731 浏览 0 评论 0原文

我想知道如何对这个类别列表进行排序(我在这里遵循了本教程 http://www.devinrolsen.com/magento-custom-category-listing-block/)在magento中按管理面板中的位置排列?目前是按id排序

<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
    <li>
        <?php
            $category = Mage::getModel('catalog/category')->load($catId);
            echo '<a href="' . $category->getUrl() . '">';
            echo $category->getName() . '</a>';
        ?>
    </li>
<?php endforeach; ?>
</ul>

I would like to know how to sort this list of categories (I followed this tutorial here http://www.devinrolsen.com/magento-custom-category-listing-block/) in magento by position in the admin panel? Currently it is sorted by id

<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
    <li>
        <?php
            $category = Mage::getModel('catalog/category')->load($catId);
            echo '<a href="' . $category->getUrl() . '">';
            echo $category->getName() . '</a>';
        ?>
    </li>
<?php endforeach; ?>
</ul>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扶醉桌前 2024-11-06 21:54:57

你为自己处理 ID 之类的事情做了太多的工作。以下内容已按标准按位置排序。

<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
    <li>
        <a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a>
    </li>
<?php endforeach; ?>
</ul>

You're making way too much work for yourself trying to deal with IDs and stuff. The following is already sorted by position as standard.

<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
    <li>
        <a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a>
    </li>
<?php endforeach; ?>
</ul>
凉栀 2024-11-06 21:54:57

如果您想按 adminhtml 中创建的位置对类别进行排序,则可以在指定所需内容的位置进行查询,因为 catalog/categoryMage_Catalog_Model_Resource_Category_Collection 的实例选择、过滤和/或排序。

这里的情况是从catalog_category_entity获取类别,仅选择名称,在ID之后进行过滤,并对位置的查询进行排序

<?php 
 $subcategories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $categoryId)
->addAttributeToSort('position', ASC);
?>

If you want to sort the categories by the position created in adminhtml you can then, since catalog/category is an instance of Mage_Catalog_Model_Resource_Category_Collection, make a query where you specify what you want to select, filter and/or sort.

The case here is getting categories from catalog_category_entity select only the name, filtering after the id and sort the query on the position.

<?php 
 $subcategories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $categoryId)
->addAttributeToSort('position', ASC);
?>
后来的我们 2024-11-06 21:54:57

这就是我所做的:

$allCategories = Mage::getModel('catalog/category');
$CategoriesTree = $allCategories->getTreeModel()->load();
$categoriesIds = 
$CategoriesTree->getCollection()->addAttributeToSort('position', 'asc')->getAllIds();

检索类别之后:

$categoryChildren = array();    

if ($categoriesIds) {
    foreach ($categoriesIds as $categoryId){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $categoryChildren[] = $category;
    }
}

然后:

// Sort by position
function comparePosition($a, $b) {
    if ($a->position == $b->position)
        return 0;
        return ($a->position > $b->position) ? 1 : -1;
    }

usort($categoryChildren, 'comparePosition');

反转返回值(1 和 -1)显然会改变顺序。
它对我来说效果很好。
希望它能帮助某人。

This is what I did:

$allCategories = Mage::getModel('catalog/category');
$CategoriesTree = $allCategories->getTreeModel()->load();
$categoriesIds = 
$CategoriesTree->getCollection()->addAttributeToSort('position', 'asc')->getAllIds();

after to retrieve the categories:

$categoryChildren = array();    

if ($categoriesIds) {
    foreach ($categoriesIds as $categoryId){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $categoryChildren[] = $category;
    }
}

and then:

// Sort by position
function comparePosition($a, $b) {
    if ($a->position == $b->position)
        return 0;
        return ($a->position > $b->position) ? 1 : -1;
    }

usort($categoryChildren, 'comparePosition');

Inverting the return (1 and -1) would obviously change the order.
It worked just fine for me.
Hope it helps someone.

无法回应 2024-11-06 21:54:57

我强烈建议先看看这里 http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections 以及知识库中的其他文章是任何 magento 开发人员必读的内容。

<?php
$cats = Mage::getModel('catalog/category')->addAttributeToSort('yourfield', 'desc')->getCollection()->getChildren();
$catIds = explode(',',$cats);
?>

I strongly suggest to lok here first http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections and also other articles in knowledge base are a must read for any magento dev.

<?php
$cats = Mage::getModel('catalog/category')->addAttributeToSort('yourfield', 'desc')->getCollection()->getChildren();
$catIds = explode(',',$cats);
?>
老娘不死你永远是小三 2024-11-06 21:54:57
    <?php
    $model_category =   Mage::getModel('catalog/category')->load($_category->getEntityId());
    $sub_categories     =   $model_category->getCollection();
    $sub_categories     ->  addAttributeToSelect('url_key')
                        ->  addAttributeToSelect('name')
                        ->  addAttributeToFilter('is_active',1)
                        ->  addIdFilter($model_category->getChildren())
                        ->  setOrder('position', 'ASC')
                        ->  load();
    ?>
    <?php   foreach($sub_categories->getData()  as  $each_subcat):  ?>
        <?php   $model_subcat   =   Mage::getModel('catalog/category')->load($each_subcat['entity_id']);    ?>                              
    <?php   endforeach; ?>
    <?php
    $model_category =   Mage::getModel('catalog/category')->load($_category->getEntityId());
    $sub_categories     =   $model_category->getCollection();
    $sub_categories     ->  addAttributeToSelect('url_key')
                        ->  addAttributeToSelect('name')
                        ->  addAttributeToFilter('is_active',1)
                        ->  addIdFilter($model_category->getChildren())
                        ->  setOrder('position', 'ASC')
                        ->  load();
    ?>
    <?php   foreach($sub_categories->getData()  as  $each_subcat):  ?>
        <?php   $model_subcat   =   Mage::getModel('catalog/category')->load($each_subcat['entity_id']);    ?>                              
    <?php   endforeach; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文