在 Magento 中向属性选项添加新值

发布于 2024-10-18 14:13:54 字数 60 浏览 8 评论 0原文

我正在尝试使用脚本向 Magento 中的属性选项添加新值以加快该过程,因为我有 2,000 多个制造商。

I am trying to add new values to an attribute option in Magento using a script to speed up the process since I have over 2,000 manufacturers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

乖乖哒 2024-10-25 14:13:54

这是我用来执行此任务的一段代码。创建一个自定义模块(使用 ModuleCreator 作为工具),然后创建一个 mysql4 -install-0.1.0.php 在 sql/modulename_setup 文件夹下。它应该包含以下内容(当然,适应您自己的数据!)。

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$aManufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer');

for($iCount=0;$iCount<sizeof($aManufacturers);$iCount++){
   $aOption['value']['option'.$iCount][0] = $aManufacturers[$iCount];
}
$installer->addAttributeOption($aOption);

$installer->endSetup();    

如果需要,请参阅 Magento wiki 的更多文档。

如果您不想在自定义模块中执行此操作,您可以创建一个以以下内容开头的 php 文件:

require_once 'app/Mage.php';
umask(0);
Mage::app('default');

Here is a piece of code that I used to perform exactly this task. Create a custom module (using ModuleCreator as a tool) and then create a mysql4-install-0.1.0.php under the sql/modulename_setup folder. It should contain the following (adapted to your own data, of course!).

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$aManufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer');

for($iCount=0;$iCount<sizeof($aManufacturers);$iCount++){
   $aOption['value']['option'.$iCount][0] = $aManufacturers[$iCount];
}
$installer->addAttributeOption($aOption);

$installer->endSetup();    

More documentation on the Magento wiki if you want.

If you don't want to do it in a custom module, you could just create a php file that starts with:

require_once 'app/Mage.php';
umask(0);
Mage::app('default');
﹎☆浅夏丿初晴 2024-10-25 14:13:54

乔纳森的回答是正确的。但是,如果您想在没有安装程序的情况下执行它,即在任何其他代码中,那么您可能会发现这很有帮助:

$arg_attribute = 'manufacturer';
$manufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');

$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;
foreach ($manufacturers as $key=>$manufacturer) {
    $option['value'][$key.'_'.$manufacturer][0] = $manufacturer;
}

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

可以找到更多信息此处

Answer of Jonathan is correct. But if you want to perform it without installer i.e in any other code, then you might find this helpful:

$arg_attribute = 'manufacturer';
$manufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');

$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;
foreach ($manufacturers as $key=>$manufacturer) {
    $option['value'][$key.'_'.$manufacturer][0] = $manufacturer;
}

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

More information can be found here.

触ぅ动初心 2024-10-25 14:13:54

我创建了一个函数来动态向属性添加选项

public function addAttributeOptions($attributeCode, $argValue)
{
    $attribute = Mage::getModel('eav/config')
        ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);


    if ($attribute->usesSource()) {

        $id = $attribute->getSource()->getOptionId($argValue);
        if ($id) 
            return $id;
    }

    $value = array('value' => array(
            'option' => array(
                    ucfirst($argValue),
                    ucfirst($argValue)
                )
        )
    );

    $attribute->setData('option', $value);
    $attribute->save();

    //return newly created option id
    $attribute = Mage::getModel('eav/config')
        ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
    if ($attribute->usesSource()) {
        return $attribute->getSource()->getOptionId($argValue);
    }
}

您可以通过提供代码和选项值向属性添加选项

$this->addAttributeOptions('unfiorm_type', 'leotartd')

I have created a function to dynamically add option to attribute

public function addAttributeOptions($attributeCode, $argValue)
{
    $attribute = Mage::getModel('eav/config')
        ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);


    if ($attribute->usesSource()) {

        $id = $attribute->getSource()->getOptionId($argValue);
        if ($id) 
            return $id;
    }

    $value = array('value' => array(
            'option' => array(
                    ucfirst($argValue),
                    ucfirst($argValue)
                )
        )
    );

    $attribute->setData('option', $value);
    $attribute->save();

    //return newly created option id
    $attribute = Mage::getModel('eav/config')
        ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
    if ($attribute->usesSource()) {
        return $attribute->getSource()->getOptionId($argValue);
    }
}

You can add an option to your attribute by providing code and option value

$this->addAttributeOptions('unfiorm_type', 'leotartd')
孤单情人 2024-10-25 14:13:54

重要!(希望这对某人有帮助,因为我在这个问题上被困了 2 个小时)

如果您使用特殊字符(例如 ä、ö、ü、ß、×、...),请确保对它们进行正确编码!

array_walk($manufacturers , create_function('&$val', '$val = utf8_encode($val);'));

Important! (Hopefully this helps somebody, cause I was stuck like 2h with this issue)

If you are using special characters (such as ä, ö, ü, ß, ×, ...) make sure to encode them properly!

array_walk($manufacturers , create_function('&$val', '$val = utf8_encode($val);'));
泛滥成性 2024-10-25 14:13:54

这是一个简单且非常快速的方法...

删除选项

/* My option value */
$value = 'A value';
/* Delete an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID from value...
    if ($option_id) {
        $options['delete'][$option_id] = true; 
        $attribute->setOption($options)->save();
    }
}
/* END ! */

添加或更新选项

/* Add/Update an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID...
    $options['order'][$option_id] = 10; // Can be removed... Set option order...
    $options['value'][$option_id] = array(
        0 => $value, // Admin Store - Required !
        1 => $value, // Store id 1 - If U want ! Can be removed
    );
    $attribute->setDefault(array($option_id)); /* If you want set option as default value */
    $attribute->setOption($options)->save(); /* That's all */
}
/* END ! */  

Here a simple and very fast way....

Delete an option

/* My option value */
$value = 'A value';
/* Delete an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID from value...
    if ($option_id) {
        $options['delete'][$option_id] = true; 
        $attribute->setOption($options)->save();
    }
}
/* END ! */

Add or update an option

/* Add/Update an option */
$options = array();
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
if ($attribute && $attribute->usesSource()) {
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID...
    $options['order'][$option_id] = 10; // Can be removed... Set option order...
    $options['value'][$option_id] = array(
        0 => $value, // Admin Store - Required !
        1 => $value, // Store id 1 - If U want ! Can be removed
    );
    $attribute->setDefault(array($option_id)); /* If you want set option as default value */
    $attribute->setOption($options)->save(); /* That's all */
}
/* END ! */  
夏了南城 2024-10-25 14:13:54

Arvind Bhardwaj 的回答在此处输入代码是正确的。但是,如果您想在没有安装程序的情况下执行它,即在任何其他代码中,那么您可能会发现这很有帮助:

同意 Arvind 但它仅适用于插入单个选项值,如果您想执行插入多个选项值,那么您只需要替换“$option['value'][$key.''.$manufacturer] = $manufacturer;”中的代码到“$option['values'][$key.''.$manufacturer] = $manufacturer;”对此。

下面是最终的代码

            require_once 'app/Mage.php';
            umask(0);
            Mage::app('default');

            $arg_attribute = 'manufacturer';
            $manufacturers = array('Sony', 'Philips', 'Samsung', 'LG', 'Panasonic', 'Fujitsu', 'Daewoo', 'Grundig', 'Hitachi', 'JVC', 'Pioneer', 'Teac', 'Bose', 'Toshiba', 'Denon', 'Onkyo', 'Sharp', 'Yamaha', 'Jamo');

            $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;
            foreach ($manufacturers as $key => $manufacturer) {
                $option['values'][$key . '_' . $manufacturer] = $manufacturer;
            }

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

,我希望它适用于插入多个选项。

Answer of Arvind Bhardwaj enter code here is correct. But if you want to perform it without installer i.e in any other code, then you might find this helpful:

Agree with Arvind but it's only works for insert the single option value and if you want to perform insert multiple option value then you just needs to replace the code from "$option['value'][$key.''.$manufacturer] = $manufacturer;" to "$option['values'][$key.''.$manufacturer] = $manufacturer;" to this.

below is the final code

            require_once 'app/Mage.php';
            umask(0);
            Mage::app('default');

            $arg_attribute = 'manufacturer';
            $manufacturers = array('Sony', 'Philips', 'Samsung', 'LG', 'Panasonic', 'Fujitsu', 'Daewoo', 'Grundig', 'Hitachi', 'JVC', 'Pioneer', 'Teac', 'Bose', 'Toshiba', 'Denon', 'Onkyo', 'Sharp', 'Yamaha', 'Jamo');

            $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;
            foreach ($manufacturers as $key => $manufacturer) {
                $option['values'][$key . '_' . $manufacturer] = $manufacturer;
            }

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

I hope its works for insertion multiple option.

如果没有你 2024-10-25 14:13:54

如果你想为不同的商店添加新值,你可以尝试这个

    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    $table = $installer->getTable('eav/attribute_option_value');
    $store = Mage::getModel('core/store')->load("store_code", "code");
    $sizes = ["default_value" => "store_value", "default_value_1" => "store_value_1"];
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product', 'size');
    $storeViewAttributeOptions = 
    Mage::getResourceModel('eav/entity_attribute_option_collection')
      ->setAttributeFilter($attribute->getId())
      ->setStoreFilter($store->getId())
      ->setPositionOrder()
      ->load();
    foreach ($storeViewAttributeOptions as $storeViewAttributeOption) {
        if (in_array($storeViewAttributeOption->getDefaultValue(), array_keys($sizes)) ){
        $data = [
            'option_id' => $storeViewAttributeOption->getId(),
            'store_id'  => $store->getId(),
            'value'     => $sizes[$storeViewAttributeOption->getDefaultValue()],
        ];
        $installer->getConnection()->insert($table, $data);
        }
    }
    $installer->endSetup();

If you want to add new values for differ store, you cat try this

    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    $table = $installer->getTable('eav/attribute_option_value');
    $store = Mage::getModel('core/store')->load("store_code", "code");
    $sizes = ["default_value" => "store_value", "default_value_1" => "store_value_1"];
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product', 'size');
    $storeViewAttributeOptions = 
    Mage::getResourceModel('eav/entity_attribute_option_collection')
      ->setAttributeFilter($attribute->getId())
      ->setStoreFilter($store->getId())
      ->setPositionOrder()
      ->load();
    foreach ($storeViewAttributeOptions as $storeViewAttributeOption) {
        if (in_array($storeViewAttributeOption->getDefaultValue(), array_keys($sizes)) ){
        $data = [
            'option_id' => $storeViewAttributeOption->getId(),
            'store_id'  => $store->getId(),
            'value'     => $sizes[$storeViewAttributeOption->getDefaultValue()],
        ];
        $installer->getConnection()->insert($table, $data);
        }
    }
    $installer->endSetup();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文