2

In my magento store I am creating a new product attribute option as so:

$new_name = 'my-new-option';

$_attribute =  Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'my-attribute');
$_options = $_attribute->getSource()->getAllOptions(true, true);
foreach ($_options as $opt) {
    if($opt['label'] === $new_name)
    {
        $exists = true;
        break;
    }
}

if (!$exists)
{
    $option['attribute_id'] = 141;
    $option['value'][0][0] = $new_name;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $setup->addAttributeOption($option);
}

This works but after adding the option i need to return the value/id to use further down my script. How can I do so?

2 Answers 2

2
$_attribute =  Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'my-attribute');

Get Eav option collectrion:

$optionCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setAttributeFilter($_attribute->getId());

BY Default any most of collection is filter by primary ket asc order

So you can get newly created option of this attribute

$newInsertedOption=$optionCollection>getLastItem();
var_dump($newInsertedOption->getData());
1
  • Thank you Amit. Can i ask though - this script gets called when a customers updates their account profile information (my-new-option comes from what they enter). If say customer A and customer B clicked and ran this at the same time - could getLastItem be mixed up between customers? If that makes sense?
    – odd_duck
    Commented Nov 6, 2014 at 16:26
1

just call the new product again and get the attribute from it.

1
  • I haven't assigned the attribute to any product yet at this stage
    – odd_duck
    Commented Nov 6, 2014 at 16:06

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.