【问题标题】:Programmatically updating/editing an attribute's option in Magento以编程方式更新/编辑 Magento 中的属性选项
【发布时间】:2011-09-01 18:41:21
【问题描述】:

我有一个名为“vendor_id”的属性。我有 N 个产品具有“vendor_id”作为产品的属性。当管理员添加新的“供应商”实体时,“vendor_id”属性的选项会以编程方式生成。代码是这样的:

    public function saveAction()
    {
    $data = $this->getRequest()->getPost();
    $designerName = $data['title'];

    $product = Mage::getModel('catalog/product');
    $attributes = Mage::getResourceModel('eav/entity_attribute_collection')
            ->setEntityTypeFilter($product->getResource()->getTypeId())
            ->addFieldToFilter('attribute_code', 'vendor_id')
            ->load(false);
    $attribute = $attributes->getFirstItem()->setEntity($product->getResource());
    $myresults = array ('value'=> array('optionone'=>array($designerName))); 

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

这暂时有效。它将为“vendor_id”属性创建一个选项,当用户添加新产品(或编辑现有产品)时,“vendor_id”的下拉菜单将由我们在 saveAction 上创建的这些“vendor”实体填充() 方法。

现在,如果管理员想要编辑现有属性选项,我不想创建新选项,我想编辑现有选项。当我们更改名称/标签时,选项 ID 保持不变很重要。

我已经挂钩了 newAction 来设置一个静态变量,所以在 saveAction() 中我们可以检查我们是否正在编辑或创建一个新选项:

    if (null == MyController::$_editScope)
    {
        error_log('Need to update option attribute');
        $attribute->addData($myresults);
    }

问题是 addData() 方法就是这样做的,它添加数据,但不更新现有的。属性为:

    $attribute = $attributes->getFirstItem()->setEntity($product->getResource());

这是一个实例:http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Attribute.html

它有 3x 父类,我已经查看了所有这些类,以寻找一种方法,可以让我编辑*或更新*现有选项的名称...

【问题讨论】:

    标签: php magento frameworks attributes entity-attribute-value


    【解决方案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;
    }
    

    【讨论】:

    • 如何为特定商店视图设置标签?
    • 第一个链接失效,在here找到相同的内容
    • @Isaias That link 现在可以使用了。感谢您的通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2010-09-30
    相关资源
    最近更新 更多