Create Attribute Options Magento 1

//Reading existing options function readExistingAttribute( $attributeCode ) { $attr = Mage::getModel(‘eav/config’)->getAttribute(‘catalog_product’, $attributeCode ); $options = $attr->getSource()->getAllOptions(); $optionsExists = array(); foreach($options as $option) { $optionsExists[] = $option[‘label’]; } return $optionsExists; } // Compare with Existing options function cmpOptions( $optionsValues, $optionsExists ) { $newOptions = array_diff( $optionsValues, $optionsExists ); return $newOptions; } function createAttribute( $options , $attributeCode)… Read more »

//Reading existing options
function readExistingAttribute( $attributeCode ) {
    
    $attr = Mage::getModel('eav/config')->getAttribute('catalog_product', $attributeCode );
    $options = $attr->getSource()->getAllOptions();
    $optionsExists = array();
    
    foreach($options as $option) {
        $optionsExists[] = $option['label'];
    }
    
    return $optionsExists;
    
}

// Compare with Existing options
 
function cmpOptions( $optionsValues, $optionsExists ) {
  
    $newOptions = array_diff( $optionsValues, $optionsExists );
    return $newOptions;
    
}

function createAttribute( $options , $attributeCode) {
    $option = array('attribute_id' => 
    Mage::getModel('eav/entity_attribute')->getIdByCode(
         Mage_Catalog_Model_Product::ENTITY, 
         $attributeCode
        )
    );

    for ($i = 0; $i < count($options); $i++) {
        
        $option['value']['option'.$i][0] = $options[ $i ]; // Store View
        $option['value']['option'.$i][1] = $options[ $i ]; // Default store view
        $option['order']['option'.$i] = $i; // Sort Order
        echo 'Insert new option : '.$options[ $i ].PHP_EOL;

    }

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

Share this Tutorial