1

I have created a custom category attribute using this tutorial http://gauss-development.com/blog/tutorials/adding-custom-category-attributes-magento/.

<?php
$installer = $this;
$installer->startSetup();
$attribute  = array(
    'type'          =>  'text',
    'label'         =>  'Extra Title',
    'input'         =>  'text',
    'global'        =>  Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'       =>  true,
    'required'      =>  false,
    'user_defined'  =>  true,
    'default'       =>  "",
    'group'         =>  "General Information"
);
$installer->addAttribute('catalog_category', 'cat_extra_title', $attribute);
$installer->endSetup();
?>

I have checked that the attribute is stored in eav_attribute table normally, but I can't get it's value.

$category = Mage::registry('current_category');
if ($category){
   $value = $category->getData('cat_extra_title'); //value is empty
}

Any ideas?

  • 1
    which page you are showing the value? – Qaisar Satti May 4 '16 at 6:28
  • 1
    First you try to print the values of the current category by using var_dump($category->getData()). Whether the values of 'cat_extra_title' is retrieving or not. – Manikandan May 4 '16 at 6:40
  • @Manikandan there are values retrieved but the 'cat_extra_title' value is not included in them. Why? – zekia May 4 '16 at 8:12
  • 2
    try $category->getCatExtraTitle(); – Manikandan May 4 '16 at 9:05
  • 1
    Can you print the values and share the link – Manikandan May 4 '16 at 9:15
3

After testing many possible solutions, this was the only one that worked for me:

$cat_extra_title = Mage::getModel('catalog/category')->load($category->getId())->getData('cat_extra_title');
  • 2
    It's actually not a good practice to call "->load" every time when you need just one attribute. First of all - if you have Category Flat enabled (check in System - Configuration - Catalog) - do a reindex of Flat Category and check if column for your attribute appeared in flat table. And second - check my reply below – Neklo.com May 5 '16 at 7:02
  • I was looking for something like this today and your solution works. Thanx – Rob D. A. May 11 '18 at 16:09
1

when adding new attribute for Category or Product, we need to put this attribute in Attribute Set and Attribute Group. So your code should look like this:

$installer->addAttribute('catalog_category', 'cat_extra_title', array(
    'type'              => 'text',
    'label'             => 'Extra Title',
    'input'             => 'text',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'required'          => false,
    'default'           => '',
    'user_defined'  =>  true,
    'default'       =>  "",
));
$entityTypeId     = $installer->getEntityTypeId('catalog_category');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'cat_extra_title'
);
0

I ran into the same issue today. I created a new custom category attribute (an int/select one), and I would try this, in list.phtml, and get an empty value:

$value = Mage::registry('current_category')->getData('my_custom_Attribute');

However, this was solved by re-indexing catalog_category_flat; with n98:

n98-magerun.phar index:reindex catalog_category_flat

Now, the same code produces the value, so long as a value exists for the category (if the value is empty/NULL, then, of course, it will still produce a NULL value).

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.