如何在 Magento 中以编程方式更新属性选项?

How to update the attribute options programmatically in Magento?(如何在 Magento 中以编程方式更新属性选项?)
本文介绍了如何在 Magento 中以编程方式更新属性选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想通过代码(以编程方式)更新/添加 Magento 中下拉属性的选项.我已经找到了如何添加属性选项,但如何更新选项值.
示例:
假设属性是制造商".我添加了三个选项 man1、man2、man3.现在通过我的自定义代码,我想将 man1 的标签更改为 man11,将 man2 的标签更改为 man22.我怎样才能做到这一点?谢谢.

I want to update/add the options of a drop down attribute in Magento though code (programmatically). I have found how to add attribute options, but how can I update the option values.
Example:
Suppose the attribute is 'manufacturer'. I have added three options man1, man2, man3. Now through my custom code I want to change the label of man1 to man11 and man2 to man22. How can I achieve that? Thanks.

推荐答案

好吧,我自己找到了解决方案.在此处查看完整详情.

Well, I found a solution myself. See complete details here.

//Get the eav attribute model
$attr_model = Mage::getModel('catalog/resource_eav_attribute');

//Load the particular attribute by id
//Here 73 is the id of 'manufacturer' attribute
$attr_model->load(73);

//Create an array to store the attribute data
$data = array();

//Create options array
$values = array(
    //15 is the option_id of the option in 'eav_attribute_option_value' table
    15 => array(
            0 => 'Apple'    //0 is current store id, Apple is the new label for the option
        ),
    16 => array(
            0 => 'HTC'
        ),
    17 => array(
            0 => 'Microsoft'
        ),
);

//Add the option values to the data
$data['option']['value'] = $values;

//Add data to our attribute model
$attr_model->addData($data);

//Save the updated model
try {
    $attr_model->save();
    $session = Mage::getSingleton('adminhtml/session');
    $session->addSuccess(
        Mage::helper('catalog')->__('The product attribute has been saved.'));

    /**
     * Clear translation cache because attribute labels are stored in translation
     */
    Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
    $session->setAttributeData(false);
    return;
} catch (Exception $e) {
    $session->addError($e->getMessage());
    $session->setAttributeData($data);
    return;
}

这篇关于如何在 Magento 中以编程方式更新属性选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)