加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 扩展Magento的EAV属性模型

发布时间:2020-12-13 18:03:23 所属栏目:PHP教程 来源:网络整理
导读:我想为Magento的EAV属性模型添加一个新属性.这可能吗? 我知道Magento允许你使用静态字段(在实体表上)扩展模型,但我想在EAV属性表本身添加一个新字段(对于目录产品属性).新属性将是一个新设置,类似于“在类别列表中可见”. 要为产品属性添加新设置,可以创建
我想为Magento的EAV属性模型添加一个新属性.这可能吗?

我知道Magento允许你使用静态字段(在实体表上)扩展模型,但我想在EAV属性表本身添加一个新字段(对于目录产品属性).新属性将是一个新设置,类似于“在类别列表中可见”.

要为产品属性添加新设置,可以创建一个扩展(1)将新列添加到catalog / eav_attribute表,(2)使用观察者在属性编辑页面中为新设置放入字段.

(1)创建新的DB字段(is_visible_in_category_list)

为您的扩展创建一个SQL脚本,并添加新列.我建议使用catalog / eav_attribute表,但你也可以使用eav_attribute:

$installer = $this;
$installer->startSetup();
$table = $installer->getTable('catalog/eav_attribute');
$installer->getConnection()->addColumn(
    $table,'is_visible_in_category_list',"TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'"
);
$installer->getConnection()->addKey(
    $table,'IDX_VISIBLE_IN_CATEGORY_LIST','is_visible_in_category_list'
);
$installer->endSetup();

在这里,我还添加了一个快速查询的索引.

(2)将字段添加到编辑产品属性页面

准备属性编辑表单时会触发一个事件,所以让我们观察它:

<events>
    <adminhtml_catalog_product_attribute_edit_prepare_form>
        <observers>
            <is_visible_in_category_list_observer>
                <class>mymodule/observer</class>
                <method>addVisibleInCategoryListAttributeField</method>
            </is_visible_in_category_list_observer>
        </observers>
    </adminhtml_catalog_product_attribute_edit_prepare_form>
</events>

然后,在观察者中添加新字段:

public function addVisibleInCategoryListAttributeField($observer)
{
    $fieldset = $observer->getForm()->getElement('base_fieldset');
    $attribute = $observer->getAttribute();
    $fieldset->addField('is_visible_in_category_list','select',array(
        'name'      => 'is_visible_in_category_list','label'     => Mage::helper('mymodule')->__('Visible in Category List'),'title'     => Mage::helper('mymodule')->__('Visible in Category List'),'values'    => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray(),));
}

就这样.由于表单中的字段名称与DB字段名称匹配,因此将自动处理从编辑页面保存设置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读