我想添加,更新和删除magento产品属性选项值和标签? 不是由管理员,而是通过编码......
提前致谢..
答案 0 :(得分:6)
Hello All,
I got it.
这里我有一个产品属性,哪个名称是制造商,我想通过编码添加新的制造商“adidas”。 所以下面是相同的片段。
我们也可以通过编码删除属性选项值。
<?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();
?>