【问题标题】:How to update the attribute options programmatically in Magento?如何在 Magento 中以编程方式更新属性选项?
【发布时间】:2012-05-02 04:50:37
【问题描述】:

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

【问题讨论】:

    标签: php magento


    【解决方案1】:

    好吧,我自己找到了解决方案。 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;
    }
    

    【讨论】:

      【解决方案2】:

      您可能想尝试扩展位于 app\code\core\Mage\Adminhtml\controllers\Catalog\Product\AttributeController.php 的 AttributeController 并覆盖 saveAction() 方法以满足您的需求。

      【讨论】:

      • 在saveAction()中,需要对属性进行所有配置。但是,我只想更新选项标签而不是任何其他配置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多