我使用以下方法将一些自定义字段添加到客户组的表单中 upgradeSchema.php.

之后我发现客户组代码和税号等原始字段是通过使用提供的API中的setter方法保存的。它与 Magento 1.X 中仅使用 setXXX() 来保存完全不同。

有帮助吗?

解决方案

在这种情况下应该使用扩展属性机制。它允许通过第三方模块扩展核心 API。启用新扩展属性的一般步骤:

  1. 声明扩展属性,如中所述 官方文档. 。清零后 var 和跑步 <project_root>/bin/magento setup:di:compile, ,这个新属性对应的 setter 和 getter 应该出现在 \Magento\Customer\Api\Data\GroupExtensionInterface (该界面是自动生成的)
  2. 为以下内容编写插件 \Magento\Customer\Api\GroupRepositoryInterface::save, \Magento\Customer\Api\GroupRepositoryInterface::getById (以及任何其他必要的服务方法)来保存/加载新属性。作为扩展开发人员,只有您知道该属性应该存储在哪里,可以是任何表。看 \Magento\Downloadable\Model\Plugin\AroundProductRepositorySave::aroundSave 举个例子
  3. 如果您需要使该属性在集合中可见(以使其可搜索/​​可过滤),请声明 join 节点。如果没有那就跳过这个
  4. 访问您的自定义属性: $customerGroup->getExtensionAttributes()->getMyAttribute(), , 在哪里 customerGroup 实施 \Magento\Customer\Api\Data\GroupInterface. setMyAttribute() 也可以使用

下面是应该放置的配置示例 VendorName/ModuleName/etc/extension_attributes.xml

<?xml version="1.0"?>
<config>
    <extension_attributes for="Magento\Customer\Api\Data\GroupInterface">
        <!--Data interface can be used as a type of attribute, see example in CatalogInventory module-->
        <attribute code="name_of_attribute" type="string">
            <resources>
                <resource ref="VendorName_ModuleName::someAclNode"/>
            </resources>
            <!--Join is optional, only if you need to have added attribute visible in groups list-->
            <join reference_table="table_where_attribute_is_stored" reference_field="group_id_field_in_that_table" join_on_field="group_id">
                <field>name_of_added_attribute_field_in_that_table</field>
            </join>
        </attribute>
    </extension_attributes>
</config>

其他提示

别忘了模块需要一个生成的register.php文件,并且必须在显示出来之前使用bin/magento module:enable VendorName_ModuleName

许可以下: CC-BY-SA归因
scroll top