代码之家  ›  专栏  ›  技术社区  ›  Thomas Müller

创建新属性不会更新产品平面数据

  •  1
  • Thomas Müller  · 技术社区  · 16 年前

    我正在编写一个导入模块,将可配置的产品导入到magento中,它工作得很好。我已经调整了导入,以便它可以创建创建可配置产品所需的所有必要属性集、属性和属性选项。到目前为止一切正常。。。一切都很好。

    导入创建新属性时,无法创建可配置产品。当我在后端编辑新属性并保存它而不做任何更改时,会出现一条消息,告诉我更新一些索引。在我更新了产品平面数据索引之后,我可以再次运行导入,一切正常。

    我尝试了创建新属性的方法:

    $setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');    
    $setup->addAttribute(
                    $this->getEntityTypeId(),
                    $code,
                    array(
                        'attribute_code'            => $code,
                        'label'                     => ucfirst($code),
                        //'group'                   => $attributeSet->getId(),
                        'user_defined'              => 1,
                        'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        'input'                     => 'select',
                        'unique'                    => 0,
                        'required'                  => 0,
                        'configurable'              => 1,
                        'filterable'                => 1,
                        'visible_on_front'          => 1,
                        'used_in_product_listing'   => 1,
                        'frontend_label'            => array(
                            $code
                        )
                    )
                );
    

    $attribute = Mage::getModel("catalog/resource_eav_attribute");
            $attribute->addData(
                array(
                    'entity_type_id'            => $this->getEntityTypeId(),
                    'attribute_code'            => $code,
                    'label'                     => ucfirst($code),
                    //'group'                   => $attributeSet->getId(),
                    'is_user_defined'           => 1,
                    'is_global'                 => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                    'frontend_input'            => 'select',
                    'is_unique'                 => 0,
                    'is_required'               => 0,
                    'is_configurable'           => 1,
                    'is_filterable'             => 1,
                    'is_visible_on_front'       => 1,
                    'is_used_in_product_listing'=> 1,
                    'frontend_label'            => array(
                        $code
                    )
                )
            );
    
            $attribute->save();
    

    两个代码都很好地创建了属性,但是我不能用它来创建可配置的属性。我试图手动运行索引脚本,但这对我没有帮助。

    我做错什么了?创造新的属性是不是魔法师的魔法?:-D个

    1 回复  |  直到 13 年前
        1
  •  0
  •   Thomas Müller    16 年前

    我忘记将新属性的后端类型设置为“int”,所以它被自动设置为static(这意味着magento在实体表中查找它)。

    另一件事是,我不能像这样设置属性值:

    $model->setData("newattributecode", 123);
    

    Magento只是没有保存属性。相反,我不得不使用未记录的函数 Mage_Catalog_Model_Product::addAttributeUpdate() 要保存此模型的属性值,请执行以下操作:

    $model->addAttributeUpdate("newattributecode", 123, 0);