0

I have a function that retrieves menu data from Magento and outputs it to a javascript front-end.

  public function getMenuData()
    {
        if (!is_null($this->_menuData)) return $this->_menuData;
        $blockClassName = Mage::getConfig()->getBlockClassName('custommenu/navigation');
        $block = new $blockClassName();
        $categories = $block->getStoreCategories();
        if (is_object($categories)) $categories = $block->getStoreCategories()->getNodes();
        if (Mage::getStoreConfig('custom_menu/general/ajax_load_content')) {
            $_moblieMenuAjaxUrl = str_replace('http:', '', Mage::getUrl('custommenu/ajaxmobilemenucontent'));
            $_menuAjaxUrl = str_replace('http:', '', Mage::getUrl('custommenu/ajaxmenucontent'));
        } else {
            $_moblieMenuAjaxUrl = '';
            $_menuAjaxUrl = '';
        }
        $this->_menuData = array(
            '_block'                        => $block,
            '_categories'                   => $categories,
            '_moblieMenuAjaxUrl'            => $_moblieMenuAjaxUrl,
            '_menuAjaxUrl'                  => $_menuAjaxUrl,
            '_showHomeLink'                 => Mage::getStoreConfig('custom_menu/general/show_home_link'),
            '_popupWidth'                   => Mage::getStoreConfig('custom_menu/popup/width') + 0,
            '_popupTopOffset'               => Mage::getStoreConfig('custom_menu/popup/top_offset') + 0,
            '_popupDelayBeforeDisplaying'   => Mage::getStoreConfig('custom_menu/popup/delay_displaying') + 0,
            '_popupDelayBeforeHiding'       => Mage::getStoreConfig('custom_menu/popup/delay_hiding') + 0,
            '_rtl'                          => Mage::getStoreConfig('custom_menu/general/rtl') + 0,
            '_mobileMenuEnabled'            => Mage::getStoreConfig('custom_menu/general/mobile_menu') + 0,
            '_mobileMenuWidthInit'          => Mage::getStoreConfig('custom_menu/general/mobile_menu_width_init') + 0,
        );
        return $this->_menuData;
    }

I've been trying to find a way to modify it so that I can alter the depth of the menu being initially displayed.

I've tried changing the $categories variable over to a call to

Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('level', array('eq'=>3))->addAttributeToFilter('parent_id',array('eq' => $parent_cat_id))->load();

to try and force it to show categories at level 3 whose parent is set by the category ID, but for the life of me I just cant crack this at the moment.

2 Answers 2

1

To get all children for a given category $parent_cat_id, use this code:

// load parent if you only have the ID:
$parent = Mage::getModel('catalog/category')->load($parent_cat_id);
// get array of child categories (not a collection!)
$categories = $parent->getChildrenCategories()

All children of a category with level 2 are level 3, so no additional filtering of the level is necessary.

3
  • that worked immediately, the only downside is that it's not displaying sub-categories, or displaying when in the top level (but the latter I can solve) Commented Sep 7, 2015 at 15:24
  • OK it was not clear to me that you wanted to load subcategories as well. Maybe this answer helps you then: magento.stackexchange.com/a/79165/243 Commented Sep 7, 2015 at 15:27
  • I've had a peek into this, it seems the function is returning /n instead of the subcategory data, I'm new to objects and I think it could be because categories is assigning data to the $parent class, not the block? Commented Sep 7, 2015 at 16:52
1

In the end it was simpler to create a new menu using existing Magento calls, rather than reverse engineering the existing, however a few bits of code from fscmengler did help in creating this!

I'll try and remove the markdown code to display the structure I came up with in the end as it works quite well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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