0

I have added custom attribute for Categories section .I want to fetch specific categories only based on custom attribute value in header . I want to modify this code :

$this->getChildHtml('topMenu') 

$_menu = $this->getHtml('level-top')

I found this method getHtml() , but I am not understanding where to alter query to get specific categories.

  • you need to modify the block file of this. – Dhiren Vasoya Jan 10 '17 at 6:57
1

TL;DR

you need to override method _getDefaultCollection in Mage_Catalog_Model_Resource_Category_Tree :

protected function _getDefaultCollection($sorted = false)
{
    $this->_joinUrlRewriteIntoCollection = true;
    $collection = Mage::getModel('catalog/category')->getCollection();
    /** @var $collection Mage_Catalog_Model_Resource_Category_Collection */
    // your filters here
    $collection = $collection->addAttributeToFilter('attribute_code', 'attribute_value');

 ...
 }

More detailed description:

The category tree rendering on frontend starts in app/code/core/Mage/Page/Block/Html/Topmenu.php

when event page_block_html_topmenu_gethtml_before is fired:

public function getHtml($outermostClass = '', $childrenWrapClass = '')
{
    Mage::dispatchEvent('page_block_html_topmenu_gethtml_before', array(
        'menu' => $this->_menu,
        'block' => $this
    ));
...

This event is listened by observer in Mage_Catalog_Block_Category::addCatalogToTopmenuItems which calls method getStoreCategories of catalog/category helper:

public function addCatalogToTopmenuItems(Varien_Event_Observer $observer)
{
    ...
    $this->_addCategoriesToMenu(
        Mage::helper('catalog/category')->getStoreCategories(), $observer->getMenu(), $block
    );
}

Which eventually leads to :

public function getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)
{
    ...
    $storeCategories = $category->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
    ...
}

Where getCategories method is lead to Mage_Catalog_Model_Resource_Category_Tree resource model method.

  • Can you please tell me what is the use of page_block_html_topmenu_gethtml_before & page_block_html_topmenu_gethtml_after dispatch event ? & how did u understand that observer is Mage_Catalog_Block_Category::addCatalogToTopmenuItems? – user7193478 Jan 10 '17 at 7:17
  • page_block_html_topmenu_gethtml_before is used in core to add category elements into a tree and page_block_html_topmenu_gethtml_after for custom purposes, and as far as I know not used in system. considering observer and addCatalogToTopmenuItems method, i looked into a code and when i did not found collection calls in it, i searched config.xml in modules for string page_block_html_topmenu_gethtml_before (I used in phpstorm find in path ) – Ilja Jan 10 '17 at 8:01
  • & how did you find getCategories belong to Mage_Catalog_Model_Resource_Category_Tree? – user7193478 Jan 10 '17 at 8:23
  • take a look into Mage_Catalog_Model_Category::getCategories it is calling Mage_Catalog_Model_Resource_Category::getCategories which is calling Mage::getResourceModel('catalog/category_tree')->addCollectionData() which is calling $this->addCollectionData() and from then skipping couple others methods it is calling _getDefaultCollection method – Ilja Jan 10 '17 at 8:34
  • Thank you so much!you explained really well.Can I ask you one more question? – user7193478 Jan 10 '17 at 8:37

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.