0

I've created a custom attribute for catalog categories through a installer script. I am pretty sure I had the script ran correctly. Here's my installer script:

$installer = $this;

$entityTypeId     = $installer->getEntityTypeId('catalog_category');

$installer->addAttribute('catalog_category', 'special_description',  array(
    'type'     => 'text',
    'label'    => 'Special Description',
    'input'    => 'text',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'default'           => '',
    'group'             =>  'General'
));

I can see the attribute in the Magento Category admin. I can set a value to my attribute and save it.

However, for some reason, I can never get the value in code. This is what I did to get the value of my attribute:

$categoryAttr = Mage::getModel('catalog/category')->load(57)->getData('special_description');

This keeps returning null. I've flushed my cache and reindex the database. But none of these have worked. I can see the value stored in the EAV tables in my database. The attribute is also created in the database. I've spent hours on this but can never understand what's wrong.

How should I get the data of my custom attribute?

1 Answer 1

0

You probably have used wrong setup class or forgot to add attribute to Default attribute group. Here is my code:

<?php 
$installer = Mage::getResourceModel('catalog/setup', 'default_setup');
$installer->startSetup();

$entityTypeId     = $installer->getEntityTypeId('catalog_category');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('catalog_category', 'special_description',  array(
    'type'     => 'text',
    'label'    => 'Special Description',
    'input'    => 'text',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
));


$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'special_description',
    '12'                    //last Magento's attribute position in General tab is 10
);

$installer->endSetup();
5
  • What should I put for the source if my attribute is of a text value? I can see yours is of a Boolean.
    – xenon
    Commented Jun 23, 2016 at 13:15
  • check my post again, I have changed it a little bit
    – Alexweb
    Commented Jun 23, 2016 at 13:18
  • Another thing is, I had the default_setup in my config.xml. I thought since I was running it through the installer script, it is going to read from the config file?
    – xenon
    Commented Jun 23, 2016 at 13:18
  • In my example I initiate installer in install script directly so it will override values from config. Also check catalog_category_flat_* tables in database. Your attribute should presented in one of these tables
    – Alexweb
    Commented Jun 23, 2016 at 13:22
  • Strangely, I tried your code and it still doesn't work. The attribute gets created in the database. But when I var_dump the Mage::getModel('catalog/category')->load(57)->getData(), I don't even see my attribute on the list. :/
    – xenon
    Commented Jun 23, 2016 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.