如何通过编码在magento中添加属性选项标签和值


How to add attribute option label and value in magento by coding?

我想添加,更新和删除magento产品属性选项值和标签?不是通过管理,而是通过编码....

Thanks in advance.

Hello All,
I got it.

这里我有一个产品属性,名称是manufacturer,我想通过编码添加新的制造商"adidas"。下面是same的代码片段

也可以通过编码来删除属性选项值。

<?php
// add new option in manufacturer attribut by coding 'adidas' in manufacturer attribute
$attribute_code=Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', "manufacturer");
$attributeInfo = Mage::getModel('eav/entity_attribute')->load($attribute_code);
$attribute_table = Mage::getModel('eav/entity_attribute_source_table')->setAttribute($attributeInfo);
$options = $attribute_table->getAllOptions(false);
 //$options = $attributeInfo->getSource()->getAllOptions(false);
$_optionArr = array('value'=>array(), 'order'=>array(), 'delete'=>array());
foreach ($options as $option){
    $_optionArr['value'][$option['value']] = array($option['label']);
    $checkarray[] = $option['label'];
    }
if (!in_array('adidas', $checkarray)) {
    $_optionArr['value']['option_1'] = array('adidas');
    $attributeInfo->setOption($_optionArr);
    $attributeInfo->save();
}
//Delete any option from manufacturer like 'adidas'
$attribute_code=Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', "manufacturer");
$attributeInfo = Mage::getModel('eav/entity_attribute')->load($attribute_code);
$attribute_table = Mage::getModel('eav/entity_attribute_source_table')->setAttribute($attributeInfo);
$options = $attribute_table->getAllOptions(false);
//$options = $attributeInfo->getSource()->getAllOptions(false);
$_optionArr = array('value'=>array(), 'order'=>array(), 'delete'=>array());
foreach ($options as $option){
                $_optionArr['value'][$option['value']] = array($option['label']);
                    if('adidas' == $option['label']){
                    $_optionArr['delete'][$option['value']] = true;
                    }
                }
$attributeInfo->setOption($_optionArr);
$attributeInfo->save();
?>