I am trying to add menu items in Magento using an observer and I have managed that but now I need to sort the order of the menu items by an attribute inside of each menuelement object (the attribute is position so I want to sort it so position 1 comes first, 2 comes second etc.). I have tried usort but can't get it to work

public function addItemsToTopmenuItems($observer)
    $menu = $observer->getMenu();
    $tree = $menu->getTree();

    $collection = Mage::getModel('sean_menucreator/menuelement')->getCollection();
    $menuelementNodeId = 'menuelement'; 

    foreach($collection as $menuitem) {
            $node = new Varien_Data_Tree_Node(array(
                'name'   => $menuitem->getName(),
                'id' => $menuelementNodeId.'_'.$menuitem->getEntity_id(),
                'url'    => $menuitem->getUrl(), // point somewhere
            ), 'id', $tree, $menu);

            $parent_id = $menuitem->getEntity_id();

            $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
            foreach ($collection as $menuitem){
                if(isset($parent_id) && $menuitem->getParent_id() == $parent_id) {
                    $tree = $node->getTree();
                    $data = array(
                        'name' => $menuitem->getName(),
                        'id' => $menuelementNodeId.'_'.$menuitem->getEntity_id(),
                        'url' => $menuitem->getUrl(),
                        'is_active' => ($action == 'sean_menucreator_menuelement_index' || $action == 'sean_menucreator_menuelement_view')

                    $menuelementNode = new Varien_Data_Tree_Node($data, 'id', $tree, $node);

    return $this;

As you can see $collection is a collection of menuelements and each menuelement is an object - is the way to do this somehow sort the initial collection before I do my foreach's? therefore initially the collection will all be in order so it will add them in order?


please try ksort

for example

$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);

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

I hope this will help you.

  • This wouldn't work, ksort sorts based on keys, not values and the key will always be 'position' and do remember I'm not sorting an array, I'm sorting an object based on its attributes
    – seanyt123
    Jan 5 '16 at 9:48
  • which attribute you want to sort on? Jan 5 '16 at 9:48
  • position, every $menuitem needs to be added to the menu via the Varien_Data_Tree_Node logic in order of it's 'position'
    – seanyt123
    Jan 5 '16 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.