Magento Attributes – Adding Attribute Options Programmatically

attributescachecustom-options

I'm creating Magento attribute options via a script, but I need to then be able to get the new ID and use it straight away in the same script.

At the moment it's not pulling the id through – if I kill the script and re-start it it picks up the created option and returns the ID, but not as part of the same script.

Here is the code I am using:

       $attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
       if ($attr->usesSource()) {
               $vattr_id = $attr->getSource()->getOptionId($value);
       }else{
               echo "No Source";
               $vattr_id = false;
       }


    if($vattr_id){
            return $vattr_id;
    }else{

            $attr_model = Mage::getModel('catalog/resource_eav_attribute');
            $attr = $attr_model->loadByCode('catalog_product', $key);
            $attr_id = $attr->getAttributeId();

            $option['attribute_id'] = $attr_id;
            $option['value'][$value][0] = $value;
            $option['value'][$value][1] = $value;

            $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
            $setup->addAttributeOption($option);
            $attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
            if ($attr->usesSource()) {
                   $vattr_id = $attr->getSource()->getOptionId($value);
                    echo "AttrID: $vattr_id";
            }

    }

Running this (with the required Mage::app() etc), creates the option, you can see it in the Magento backend, but the $vattr_id is NULL. If I reload the script, then it finds the attribute option in that first block as it should.

I guess it's something to do with how Magento is caching the models, but not sure where I need to look to clear these?

Thanks!

Best Answer

I am using slightly different approach to save attribute option value

$arg_value = 'your option label';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'your attribute code');
        $flag=0;

    foreach ( $attribute->getSource()->getAllOptions(true, true) as $option )
    {

        if($arg_value == $option['label'])
        {

            unset($attribute);
            $flag=1;
            return $option['value'] ; 
        }
    }
    if($flag==0){
        $attribute_model        = Mage::getModel('eav/entity_attribute');
        $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;

        $attribute_code         = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
        $attribute              = $attribute_model->load($attribute_code);

        $attribute_table        = $attribute_options_model->setAttribute($attribute);
        $options                = $attribute_options_model->getAllOptions(false);

        $value['option'] = array($arg_value,$arg_value);
        $result = array('value' => $value);
        $attribute->setData('option',$result);
        $attribute->save();

        $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $arg_attribute);
        foreach ( $attribute->getSource()->getAllOptions(true, true) as $option )
        {
            if($arg_value == $option['label'])
            {
                unset($attribute);
                return $option['value'] ; 
            }
        }

    }

this will create new option value if it not exist and return id of option.

Related Topic