How to sort an array of category list alphabetically in Magento

In Magento, I created a phtml template file with the code below. I got this from this tutorial . I and others are interested in how to sort this list of categories in alphabetical order. The first lines of code create an array with category identifiers. Next, we can get the category name using the identifier in the foreach section. But to sort by name, we need to get the names in the array before foreach, and then sort them by name. How?

<?php
$cats = Mage::getModel('catalog/category')->load(319)->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>

Note. 319 is the category identifier of the parent category for which I want to list subcategories. In addition, I did not put this category page template. I insert as a block on the CMS page (this part is already working).

+3
5

.

<?php
$cats = Mage::getModel('catalog/category')->load(319)->getChildren();
$catIds = explode(',',$cats);

$categories = array();
foreach($catIds as $catId) {
       $category = Mage::getModel('catalog/category')->load($catId); 
       $categories[$category->getName()] = $category->getUrl();
}

ksort($categories, SORT_STRING);
?>

<ul>
<?php foreach($categories as $name => $url): ?>
    <li>
    <a href="<?php echo $url; ?>"><?php echo $name; ?></a>
    </li>
<?php endforeach; ?>
</ul>

, Magento - . (?)

+10

Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('parent_id', '319')->addAttributeToSort('name', 'ASC');

, - Varien. -, , parent_id db, , .

Magento

+19

, magento 1.4.1.1, :

$cats = Mage::getModel('catalog/category')->load(319)->getChildrenCategories();

id 319 code/core/Mage/catalog/Model/Resource/Eav/Mysql4/category.php 582 getChildrenCategories(),

->setOrder('position','ASC'); 

->setOrder('name','ASC);

, .

+1

Magento (CE 1.7.0.2) +

$children = Mage::getModel('catalog/category')->getCategories(319, 1, true, true);

// iterate through the results
foreach ($children as $category):
    echo '<option value="' . $category->getUrl() . '">' . $category->getName() . '</option>';
endforeach;

getChildren() ...

app/code/core/Mage/Catalog/Model/Category.php around line 817

. , !

+1

topmenu.phtml, topmenu.phtml

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php

function array_sort($array, $on, $order=SORT_ASC){
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
            asort($sortable_array);
            break;
            case SORT_DESC:
            arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

?>
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
?>
<div class="nav-container">
    <ul id="nav">
    <?php $_helper = Mage::helper('catalog/category') ?>
    <?php $_categories = $_helper->getStoreCategories() ?>
    <?php $currentCategory = Mage::registry('current_category') ?>
    <?php if (count($_categories) > 0){ ?>
        <?php foreach($_categories as $_category){ ?>
            <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
            <li><a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><span><?php echo $_category->getName(); ?></span></a>
            <?php $catList = array();?>
            <?php $_subcategories = $_category->getChildrenCategories() ?>
            <?php foreach($_subcategories as $_subCategory){ ?>
                <?php $catList[] = array('name' => $_subCategory->getName(), 'url' => $_subCategory->getUrl(), 'id' => $_subCategory->getId());?>
            <?php } ?>
            <?php $catList = array_sort($catList, 'name', SORT_ASC);?>
            <ul>
            <?php if (count($catList) > 0){ ?>
                <?php $subcat=0?>
                <?php foreach($catList as $_subCategory){ ?>
                    <li><a href="<?php echo $_subCategory['url'] ?>"><span><?php echo $_subCategory['name'] ?></span></a>
                    <?php $subCatList = array();?>
                    <?php $_subSubCat = Mage::getModel('catalog/category')->load($_subCategory['id']);
                    $_subSubCategories = $_subSubCat->getChildrenCategories();?>
                    <?php foreach($_subSubCategories as $_subSubCategory){ ?>
                        <?php $subCatList[] = array('name' => $_subSubCategory['name'], 'url' => $_subSubCategory['url']);?>
                    <?php } ?>
                    <?php $subCatList = array_sort($subCatList, 'name', SORT_ASC);?>
                    <?php if (count($subCatList) > 0){ ?>
                        <ul>
                            <?php foreach($subCatList as $_subSubCat){ ?>
                                <li><a href="<?php echo $_subSubCat['url'] ?>"><span><?php echo $_subSubCat['name'] ?></span></a>
                            <?php } ?>
                            </li>
                        </ul>
                    <?php } ?>
                    </li>
                <?php } ?>

                <?php } ?>
            </ul>
            </li>
        <?php } ?>
    <?php } ?>
    </ul>
</div>
+1

Source: https://habr.com/ru/post/1776823/


All Articles