【问题标题】:Sorting categories in Magento according to the position in admin根据管理员中的位置对 Magento 中的类别进行排序
【发布时间】:2011-04-07 18:46:31
【问题描述】:

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

【问题讨论】:

    标签: magento


    【解决方案1】:

    您在尝试处理 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>
    

    【讨论】:

    • 谢谢伙计,这就像一个魅力。你拯救了我的一天。是的,这是比使用猫 ID 和其他东西更清洁的方法。再次感谢;)
    • 这看起来不错,但我似乎无法获得工作版本。使用magento 1.6。我想加载活动商店的类别。
    • @Lucas - 要修改此 sn-p,请将类别 ID(在本例中为 3)替换为 Mage::app()-&gt;getStore()-&gt;getRootCategoryId()。但是还有其他方法,请查看 catalog/navigation/*.phtml 模板以了解其通常是如何完成的,或者如果您需要特定的东西,请提出一个新问题。
    • 太棒了!我还在循环中添加了条件if($subCategory-&gt;getIsActive()) {
    • @johnsnails 此处,$cats 已加载,因此无法进一步修改,仅包含“url”、“name”、“position”、“is_anchor”、“is_active”字段。要同时查看说明,您必须使用允许您致电 addAttributeToSelect() 的其他答案之一。
    【解决方案2】:

    如果您想按在 adminhtml 中创建的位置对类别进行排序,那么由于catalog/categoryMage_Catalog_Model_Resource_Category_Collection 的一个实例,您可以在其中指定要选择、过滤和/或排序的内容进行查询。

    这里的情况是从catalog_category_entity 中获取类别,只选择名称,在 id 之后过滤并排序position 上的查询。

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

    【讨论】:

    • 为什么这是一个解决方案?请解释一下。
    • 如果您使用'position' 而不是'name',则此方法有效。
    • 此答案还希望您遍历父类别。对于顶级类别,您可以使用:-&gt;addAttributeToFilter('level', 2),不带 -&gt;addFieldToFilter('parent_id', $categoryId) 行。顶级类别的级别为 2,因为它们位于根类别下,即级别 1。
    【解决方案3】:

    这就是我所做的:

    $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)显然会改变顺序。 它对我来说很好。 希望它可以帮助某人。

    【讨论】:

      【解决方案4】:

      我强烈建议先在这里查看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);
      ?>
      

      【讨论】:

      • 非常有帮助的评论 :) 你用什么字段名来排序
      • $cats = Mage::getModel('catalog/category')->addAttributeToSort('position', 'desc')->load(3)->getChildren();
      • @Anton - 这永远行不通,因为模型是模型,而不是集合。只有集合可以排序。
      • 你说得对,我忘记了可以轻松使用而不是加载的收集部分
      • 嗯,还是不行。可能位置的管理参数存储在其他地方。
      【解决方案5】:
          <?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; ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 2015-12-09
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 2011-07-22
        • 2018-04-04
        • 1970-01-01
        相关资源
        最近更新 更多