Retrieve Custom Category Attribute Value in Magento

categorymagento-1

I had a module installed in Magento in my old shop store and now I migrate to the new one. What module did is it basically added some custom text fields to the categories that were then displayed on the category page. The following code in the sql folder of the module made corresponding entries in the database:

$installer = $this;
$installer->startSetup();

$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = 4; // 4 = General Information

$installer->addAttribute('catalog_category', 'textfield_name', array(
    'type' => 'varchar',
    'label' => 'Textfield Title',
    'input' => 'text',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible' => TRUE,
    'required' => FALSE,
    'user_defined' => FALSE,
    'visible_on_front' => TRUE,
    'is_html_allowed_on_front' => FALSE,
    'default' => ''
));

$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'textfield_name',
    '30'
);

$installer->endSetup();

After module was installed I got this new field displayed on the category properties page in the new store backend. I stored some text in it and then tried to retrieve it as follows:

Mage::getSingleton('catalog/layer')->getCurrentCategory()->getTextfieldName();

but I didn't get anything. Also I made certain that Mage::getSingleton('catalog/layer')->getCurrentCategory() was indeed the right category.

There was though one significant detail that I have changed in the new store. In the layout that defines the block which runs the latter code I had to change the scope from catalog_category_layered to catalog_category_default in order for the block to be displayed. I suppose it is relevant, but I don't know what to do next.

Thank you in advance!

Best Answer

Just so this question has an answer ...based on the comments.
The category flat index needs to be refreshed.

As a general rule, when adding a new attribute to the category entity and if the flat category flag is set to yes the index needs to be rebuilt.
This happens because, the new column corresponding to the new attribute does not exist in the flat tables, and on frontend the categories are retrieved from those tables.

If the flat category flat is off, then rebuilding indexes is not mandatory.