【问题标题】:Creating new options for Magento attributes为 Magento 属性创建新选项
【发布时间】:2014-02-28 20:19:41
【问题描述】:

我在尝试在“管理选项”标签中创建新选项时遇到问题。创建属性时,我知道如何将数据正确保存在数据库中。我用我的模块替换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>

Ceicom/Swatches/Block/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');
    }
}

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

显然,要做到这一点需要在表eav_attribute_option 中添加新列。例如]:field_1field_2

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

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

【问题讨论】:

  • 您好,您需要先在渲染器文件夹下找到它的 .phtml 文件,然后您可以轻松更新两个字段。
  • Jonatan,您需要以编程方式添加新选项吗?因为它可以使用安装程序文件完成:see this question

标签: php attributes option magento


【解决方案1】:

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

一开始我试图覆盖 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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2016-02-03
    • 2021-10-13
    • 2013-12-02
    相关资源
    最近更新 更多