为Magento属性创建新选项

Jon*_*tos 3 php attributes magento option

我在尝试在"管理选项"选项卡中创建新选项时遇到问题.创建属性时,我知道如何在数据库中正确保存数据.我正在用Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options我的模块替换来创建自定义字段.

我的模块:

config.xml中

<config>
        <blocks>
            <adminhtml>
                <rewrite>
                     <catalog_product_attribute_edit_tabs>Ceicom_Swatches_Block_Adminhtml_Tabs</catalog_product_attribute_edit_tabs>
                     <catalog_product_attribute_edit_tab_options>Ceicom_Swatches_Block_Adminhtml_Options</catalog_product_attribute_edit_tab_options>
                 </rewrite>
             </adminhtml>
        </blocks>
</config>
Run Code Online (Sandbox Code Playgroud)

Ceicom /色板/座/ Adminhtml/Options.php

class Ceicom_Swatches_Block_Adminhtml_Options extends Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('ceicom/attribute/options.phtml');
    }
}
Run Code Online (Sandbox Code Playgroud)

在放置在自定义字段中的Phtml文件中:

inserir描述da imagem aqui

显然,这需要在表中添加新列eav_attribute_option.例如]:field_1,field_2.

要保存其他字段,我需要重写:Mage_Eav_Model_Resource_Entity_Attribute::_saveOption().

有关如何在不更改CORE的情况下执行此操作的任何提示,就像我上面使用的那样rewrite,以及如何加载数据库以进行编辑属性的输入?

小智 7

这是一种可能的解决方法:

在开始时我试图覆盖eav类Mage_Eav_Model_Resource_Entity_Attribute,但我没有工作.在仔细查看代码之后,我发现_saveOption方法是由另一个扩展Mage_Eav_Model_Resource_Entity_Attribute的类调用的.因此,如果您使用自定义类重写Mage_Eav_Model_Resource_Entity_Attribute,则在保存选项过程中不会产生任何影响.我也意识到还有另外一个扩展Mage_Eav_Model_Resource_Entity_Attribute的条款

那些条款是:

  • Mage_Catalog_Model_Resource_Attribute
  • Mage_Eav_Model_Mysql4_Entity_Attribute
  • Mage_Eav_Model_Resource_Attribute(摘要)

为了能够覆盖属性选项保存过程中的方法,您必须:

1)创建了一个扩展Mage_Eav_Model_Resource_Entity_Attribute并覆盖该方法的类

class My_Module_Model_Eav_Resource_Entity_Attribute extends Mage_Eav_Model_Resource_Entity_Attribute{ protected function _saveOption(Mage_Core_Model_Abstract $object){ //your custom logic here } }

不要在模块配置中覆盖Mage_Eav_Model_Resource_Entity_Attribute,上面的类将作为我们的主目标类Mage_Catalog_Model_Resource_Attribute的父级,它是参与保存过程的类.

2)在模块配置中覆盖Mage_Catalog_Model_Resource_Attribute类,其中包含一个新类,该类将扩展您之前创建的类My_Module_Model_Eav_Resource_Entity_Attribute

您的配置将如下所示

<global>
      <models>
            <!-- Overrides Mage_Catalog_Model_Resource_Attribute -->
            <catalog_resource>
                <rewrite>
                    <attribute>My_Module_Model_Catalog_Resource_Attribute</attribute>
                </rewrite>
            </catalog_resource>
        </models>
       <!-- The rest of global config section -->
</global>
Run Code Online (Sandbox Code Playgroud)

现在,您将看到在保存属性时正在执行的自定义__saveOption方法.