Magento2 给Product Attribute 扩展自定义字段 - 1

 如图我们需要给所有Attribute扩展以下2种属性

etc/adminhtml/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front">
        <plugin name="extension_attribute_edit_form" type="Pje\Catalog\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front" sortOrder="1"/>
    </type>

</config>

etc/db_schema.xml

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">


    <table name="catalog_eav_attribute" resource="default" engine="innodb" comment="Catalog EAV Attribute Table">
        <column xsi:type="smallint" name="used_tip" unsigned="true" nullable="false"
                identity="false" default="0" comment="Custom tips"/>
        <column xsi:type="smallint" name="show_mode" unsigned="true" nullable="false"
                identity="false" default="0" comment="Custom mode"/>
    </table>

</schema>

app\code\Pje\Catalog\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab\Front.php

<?php
namespace Pje\Catalog\Plugin\Block\Adminhtml\Product\Attribute\Edit\Tab;

class Front
{

    protected $custom;
    protected $_coreRegistry;

    public function __construct(
        \Magento\Config\Model\Config\Source\Yesno $custom,
        \Magento\Framework\Registry $_coreRegistry
    ) {
        $this->custom = $custom;
        $this->_coreRegistry = $_coreRegistry;
    }

    public function aroundGetFormHtml(
        \Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front $subject,
        \Closure $proceed
    )
    {
        $attributeObject = $this->getAttributeObject();
        $customSource = $this->custom->toOptionArray();
        $form = $subject->getForm();
        $fieldset = $form->getElement('front_fieldset');
        $fieldset->addField(
            'used_tip',
            'select',
            [
                'name' => 'used_tip',
                'label' => __('Used Tip'),
                'title' => __('Used Tip'),
                'value' => $attributeObject->getData('used_tip'),
                'values' => $customSource,
            ]
        );
        $fieldset->addField(
            'show_mode',
            'select',
            [
                'name' => 'show_mode',
                'label' => __('Show Mode'),
                'title' => __('Show Mode'),
                'value' => $attributeObject->getData('show_mode'),
                'values' => array(
                    array('label' => 'None', 'value' => 0),
                    array('label' => 'Pic Mode', 'value' => 1),
                    array('label' => 'Color Mode', 'value' => 2),
                    array('label' => 'Text Mode', 'value' => 3),
                    array('label' => 'Circle Mode', 'value' => 4),
                    array('label' => 'Drop Mode', 'value' => 5),
                    array('label' => 'Custom Mode', 'value' => 6),
                ),
            ]
        );
        return $proceed();
    }

    private function getAttributeObject()
    {
        return $this->_coreRegistry->registry('entity_attribute');
    }
}

最后执行命令:sh pub/shell/deploy_all.sh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值