我想以编程方式在模块的数据升级脚本中添加新产品选项值。我怎样才能做到这一点?
我想以编程方式在模块的数据升级脚本中添加新产品选项值。我怎样才能做到这一点?
Answers:
在升级脚本文件中添加以下代码
<?php
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
if ($attribute->getId() && $attribute->getFrontendInput()=='select') {
$option['attribute_id'] = $attribute->getId();
$option['value'] = array('Red','Black', 'Yellow');
$installer->addAttributeOption($option);
}
//OR
/*
if($attribute->getId() && $attribute->getFrontendInput()=='select') {
$option['attribute_id'] = $attribute->getId();
$option['value']['r'][0] = 'Red';
$option['value']['b'][1] = 'Black';
$option['value']['y'][2] = 'Yellow';
$installer->addAttributeOption($option);
}*/
$installer->endSetup();
检查重复的选项值代码:
<?php
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$attributeCode = 'manufacturer';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
if($attribute->getId() && $attribute->getFrontendInput()=='select') {
$newOptions = array('Red','Black', 'Yellow');
$exitOptions = array();
$options = Mage::getModel('eav/entity_attribute_source_table')
->setAttribute($attribute)
->getAllOptions(false);
foreach ($options as $option) {
if (in_array($option['label'], $newOptions)) {
array_push($exitOptions, $option['label']);
}else {
}
}
$insertOptions = array_diff($newOptions, $exitOptions);
if(!empty($insertOptions)) {
$option['attribute_id'] = $attribute->getId();
$option['value'] = $insertOptions;
$installer->addAttributeOption($option);
}
}
$installer->endSetup();
eav_attribute_option
会获得一个新行,但中没有对应的行eav_attribute_option_value
。必须是带有$option
数组结构的东西。
尝试这个,
对于单个值:-
$arg_attribute = 'color';
$arg_value = 'red';
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();
$option['attribute_id'] = $attr_id;
$option['value']['any_option_name'][0] = $arg_value;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
对于多个值:-
$arg_attribute = 'color';
$key_data = array('red','black','orange');
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
foreach($key_data as $key_value)
{
$option = array();
$arg_value = trim($key_value);
$attr_id = $attr->getAttributeId();
$option['attribute_id'] = $attr_id;
$option['value']['any_option_name'][0] = $arg_value;
$setup->addAttributeOption($option);
}
“ any_option_name”将是color_name(例如:红色)arg_value将是整数optionId afaik。
还需要首先获取的是下一个未使用的optionId。用于此新属性选项。
例如,您想Men
为gender
选项增加价值。
首先,您必须在模块目录(例如)中创建升级脚本app/code/local/MyCompany/MyModule/data/mymodule_setup/data-upgrade-0.1.0-0.1.1.php
。
然后用以下代码填充它:
<?php
$this->startSetup();
$genderAttribute = Mage::getModel('eav/entity_attribute')
->loadByCode('catalog_product', 'gender'); // 'gender' is your attribute code
$this->addAttributeOption([
'attribute_id' => $genderAttribute->getId(),
'value' => [[0 => 'Men', 1 => 'Men', 10 => 'Men']] // array indexes are store IDs
]);
$this->endSetup();
以下代码以编程方式添加了属性选项magento 1。
请参阅详细说明,了解如何从CSV读取并与现有属性选项进行比较 https://www.pearlbells.co.uk/add-attribute-options-magento-scripts/
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);
}
'r'
,'b'
,'y'
在$option['value']['r'][0] = 'Red';
?