Category – Getting Custom Attribute Value

category

I have created a new tab in the category edit page that gives access to a custom category attribute that has a key of mymodule_myattribute. I did this using an install script:

$installer->addAttribute(
    'catalog_category',
    'mymodule_myattribute',
    array(
        'label' => 'My Attribute',
        'group' => 'My Attribute Tab',   //will be created if necessary
        'type'  => 'int',
        'class' => 'validate-number',
        'required' => false,
        'input_renderer' => 'mymodule/adminhtml_catalog_category_widget_mycoolwidget',
    )
);

The attribute appears in the new tab and the setting gets saved when Save Category is clicked. This is all good… however…I am trying to access that custom attribute value for each category in a stand alone script:

require_once('app/Mage.php');
Mage::app('default'); 

$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('*');

foreach ($categories as $category) {

    $cat = Mage::getModel("catalog/category")->load($category->getId());
    var_dump($cat->getData('mymodule_myattribute')); // NULL
    var_dump($cat->getMymoduleMyattribute()); // NULL

    var_dump($cat->getName()); // My Cool Category Name

}

The above calls for the custom attributes return NULL even though I know it is getting saved (because Magento saves it for me).

Is there something blindingly obvious I'm missing here?

Best Answer

I programmatically updated my indexes (Thanks Marius):

require_once('app/Mage.php');
Mage::app('default'); 

$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('*');

foreach ($categories as $category) {

    $process = Mage::getModel('index/process')->load(5); $process->reindexAll();      
    $process = Mage::getModel('index/process')->load(6); $process->reindexAll(); 

    $cat = Mage::getModel("catalog/category")->load($category->getId());
    var_dump($cat->getData('mymodule_myattribute')); // Gives result
    var_dump($cat->getMymoduleMyattribute()); // Gives result

    var_dump($cat->getName()); // My Cool Category Name

}
Related Topic