0

I have created a custom category attribute in my install script by using:

$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();

$installer->addAttribute(
    'catalog_category',
    'my_custom_attribute',
    array(
        'label' => 'My Custom Attribute',
        'group' => 'Custom Attributes',   
        'type'  => 'int',
        'class' => 'validate-number',
        'required' => false,
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'       => true,
        'visible_on_front' => true, 
    )
);

$installer->endSetup();

This works fine, the category attribute is created in the database. Now, I want to set the attribute to a specific value in a stand alone script:

    <?php
    require_once('app/Mage.php');
    Mage::app('default'); 
    umask(0);

    $process = Mage::getModel('index/process')->load(5); //category EAV
    $process->reindexAll();  // Re-index 

    $cat = Mage::getModel("catalog/category")->load(1);

    $cat->setData('my_custom_attribute',   10);

    $cat->save();

    $process = Mage::getModel('index/process')->load(5); //category EAV
    $process->reindexAll();  // Re-index 

    $cat = Mage::getModel("catalog/category")->load(1);

    $val = $cat->getData('my_custom_attribute');

    var_dump($val); // NULL <- should be 10

I have tried many different variations of setting / getting the value. As you can see I am re-indexing twice because we are using Flat Data.

I have been trying to work this out for ages - how do I simply set the custom attribute value so I can re-call it in the same script?

0

After hours of fiddling and playing - I asked a colleague. We're not sure why this works though. But here you go:

    <?php
    require_once('app/Mage.php');
    Mage::app('admin');  <-- THIS LINE from 'default' to 'admin'
    umask(0);

    // ......

Painful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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