6

In this class app/code/core/Mage/Catalog/Block/Navigation.php in this method:

/**
 * Render category to html
 *
 * @param Mage_Catalog_Model_Category $category
 * @param int Nesting level number
 * @param boolean Whether ot not this item is last, affects list item class
 * @param boolean Whether ot not this item is first, affects list item class
 * @param boolean Whether ot not this item is outermost, affects list item class
 * @param string Extra class of outermost list items
 * @param string If specified wraps children list in div with this class
 * @param boolean Whether ot not to add on* attributes to list item
 * @return string
 */
protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
    $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{
    if (!$category->getIsActive()) {
        return '';
    }
    //...
}

the first parameter $category has type Mage_Catalog_Model_Category. I need to get the value of the custom category attribute inside that method. But when I try to get the category attribute value like this:

$value = $category->getData('catalog_pdf');

it returns nothing. I checked the type of the parameter $category with

get_class($category); //It returnes Varien_Data_Tree_Node

and it turned out to be Varien_Data_Tree_Node (but not Mage_Catalog_Model_Category like it is declared in the comments above this method).

So my question is:

  1. How I can retrieve an object of type Mage_Catalog_Model_Category so that it is possible to retrieve value of the category attribute? Is the standard category object wrapped somehow inside that object of type Varien_Data_Tree_Node?

  2. And a bonus question: what is that type Varien_Data_Tree_Node used for? I tried to investigate the code but that class seems to be quite complicated. If anyone knows any good tutorials about it, I'd be very grateful.

14

Try something like that:

$value = Mage::getModel('catalog/category')->load($category->getId())->getData('catalog_pdf');

EDIT:

Better way (without loading whole category model):

Mage::getResourceModel('catalog/category')->getAttributeRawValue($category->getId(), "catalog_pdf", Mage::app()->getStore()->getId());
  • 1
    This will load that category again (or isn't it? I'm not sure) so this will affect performance. But isn't that object (a category with all the data) already embeded somewhere inside $category? Do we really need to load it again? – kahirm Jun 16 '14 at 21:52
  • 1
    Yeach you've right ;D better way will be: Mage::getResourceModel('catalog/category')->getAttributeRawValue($category->getId(), "catalog_pdf", Mage::app()->getStore()->getId());. I've edited my comment ;) – kuba_ceg Jun 17 '14 at 7:52
  • Absolutely correct !! Saved a Man hour of mine :) – Chiragit007 May 5 '15 at 10:28
  • Great It save my time – Pushpendra Singh May 30 '17 at 5:58
0

I had the same problem. Reindexing made it go away. You don't need to load the whole model for that.

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.