1

Currently I have something similar to the following that creates attributes :

// $this->_catalogSetup -> Mage_Catalog_Model_Resource_Setup
$this->_catalogSetup->addAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'my_attribute_code',
    array(
        'type' => 'int',
        'filterable' => true,
        'label' => 'My Attribute',
        'input' => 'select',
        'option' => array('values' => array(
             'My Option 1',
             'My Option 2'
        )),
        'required' => false,
        'sort_order' => 1,
    )
);

However, obviously every time I run this, it create those options again. I would like this code to be executed multiple times and only ever do an "update". This works fine for the rest of the attribute's details with the exception of the options, is there a way to make it update the options, rather than add to?

2

The facility that you want does not exist in Magento's EAV setup, which should be considered as a convenience device only.

Updating existing options along with other attribute details can be achieved (in this case) by working with the catalog/resource_eav_attribute object directly.

See \Mage_Adminhtml_Catalog_Product_AttributeController::saveAction() and \Mage_Eav_Model_Resource_Entity_Attribute::_saveOption() for implementation details.

0

Please check the tutorial explain how compare with existing attribute options and add new options. http://www.pearlbells.co.uk/import-attribute-options-magento-2-programmatically/

function addNewOptions( $object_Manager, $newOptions, $storeArray ) {

$option = array();
$eavConfig = $object_Manager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'color');
$option['attribute_id'] = $attribute->getAttributeId();

foreach($newOptions as $key => $value){

    $option['value'][$value][0]=$value;

    foreach($storeArray as $storeKey => $store) {
        $option['value'][$value][$storeKey] = $value;
    }   
    echo 'Added value : '.$value.PHP_EOL;
}

$eavSetup = $object_Manager->get('\Magento\Eav\Setup\EavSetup');
$eavSetup->addAttributeOption($option);

}

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.