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?
-
get function for getting valuesAmit Bera– Amit Bera ♦2014年02月18日 11:08:06 +00:00Commented Feb 18, 2014 at 11:08
-
2Did you rebuild your indexes?Marius– Marius2014年02月18日 12:15:29 +00:00Commented Feb 18, 2014 at 12:15
-
Boooosh .. no I hadn't re-indexed! Works now, thanks Marius.beingalex– beingalex2014年02月18日 12:30:37 +00:00Commented Feb 18, 2014 at 12:30
1 Answer 1
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
}