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?

3
  • get function for getting values
    – Amit Bera
    Feb 18, 2014 at 11:08
  • 2
    Did you rebuild your indexes?
    – Marius
    Feb 18, 2014 at 12:15
  • Boooosh .. no I hadn't re-indexed! Works now, thanks Marius.
    – beingalex
    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

}
0

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.