2

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?

asked Feb 18, 2014 at 11:04
3
  • get function for getting values Commented Feb 18, 2014 at 11:08
  • 2
    Did you rebuild your indexes? Commented Feb 18, 2014 at 12:15
  • Boooosh .. no I hadn't re-indexed! Works now, thanks Marius. Commented Feb 18, 2014 at 12:30

1 Answer 1

3

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
}
answered Feb 18, 2014 at 14:18
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.