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?
-
1which page you are showing the value?Qaisar Satti– Qaisar Satti2016年05月04日 06:28:55 +00:00Commented May 4, 2016 at 6:28
-
1First 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– Manikandan2016年05月04日 06:40:13 +00:00Commented May 4, 2016 at 6:40
-
@Manikandan there are values retrieved but the 'cat_extra_title' value is not included in them. Why?zekia– zekia2016年05月04日 08:12:28 +00:00Commented May 4, 2016 at 8:12
-
2try $category->getCatExtraTitle();Manikandan– Manikandan2016年05月04日 09:05:12 +00:00Commented May 4, 2016 at 9:05
-
1Can you print the values and share the linkManikandan– Manikandan2016年05月04日 09:15:18 +00:00Commented May 4, 2016 at 9:15
3 Answers 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');
-
3It'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 belowNeklo.com– Neklo.com2016年05月05日 07:02:31 +00:00Commented May 5, 2016 at 7:02
-
I was looking for something like this today and your solution works. ThanxRob D. A.– Rob D. A.2018年05月11日 16:09:10 +00:00Commented May 11, 2018 at 16:09
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'
);
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).
Explore related questions
See similar questions with these tags.