如何以编程方式在数据升级脚本中添加属性选项值?


Answers:


12

在升级脚本文件中添加以下代码

<?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();

1
什么是指标的含义'r''b''y'$option['value']['r'][0] = 'Red';
Anton Belonovich '16

1
这会在Magento CE 1.9上创建一个残破的下拉选项。表格eav_attribute_option会获得一个新行,但中没有对应的行eav_attribute_option_value。必须是带有$option数组结构的东西。
Anse

请帮助我检查属性值(如果该值已经可用)。因此重复的值不会插入属性中
Purushotam Sharma

@Purushotam Sharma:更新ans pls检查,并让我知道它是否有效,因为我未测试代码。
阿卜杜勒

你好@阿卜杜勒!运行此脚本时未添加选项
SagarPPanchal

12

尝试这个,

对于单个值:-

$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。用于此新属性选项。


6

例如,您想Mengender选项增加价值。

首先,您必须在模块目录(例如)中创建升级脚本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();

2

以下代码以编程方式添加了属性选项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);
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.