Liam Wiltshire
Liam Wiltshire

Reputation: 1264

Adding attribute options programmatically are not available for use immediately

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 back end, 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?

Upvotes: 2

Views: 5086

Answers (1)

Victor Nazarov
Victor Nazarov

Reputation: 126

function getAttributeOptionId($attributeName, $attributeValue) {

    /* @var $attribute Mage_Eav_Model_Entity_Attribute */
    $attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName);

    // checking attribute code
    if ($attribute->getId()) {
        $source = $attribute->getSource();
        $options = $source->getAllOptions();

        // looking for existing id
        foreach ($options as $optionValue) {
            if ($attributeValue == $optionValue["label"]) {
                return $optionValue["value"];
            }
        }

        // making new option
        $addOptionData = array(
            "attribute_id" => $attribute->getId(),
            "value" => array(array($attributeValue))
        );

        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        $setup->addAttributeOption($addOptionData);

        // getting new id
        $attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName);
        $source = $attribute->getSource();
        $options = $source->getAllOptions();

        foreach ($options as $optionValue) {
            if ($attributeValue == $optionValue["label"]) {
                return $optionValue["value"];
            }
        }
    }

    return null;
}

echo getAttributeOptionId("brand", "Intel");

Upvotes: 4

Related Questions