質問

upgradeSchema.phpを使用して、カスタムグループの形式にカスタムフィールドを追加しました。

その後、私は顧客グループコードや税IDのような元のフィールドが提供されたAPIのSetterメソッドを使用して保存されていることがわかりました。SetXXX()を使用して保存するだけのMagento 1.xとはまったく異なります。

役に立ちましたか?

解決

拡張子属性メカニズムを使用する必要があります。これにより、サードパーティモジュールによるコアAPIを拡張できます。新しい拡張子属性を有効にするための一般的な手順:

  1. 公式ドキュメントに記載されているように拡張属性を宣言します。 varをクリアして<project_root>/bin/magento setup:di:compileを実行した後、この新しい属性の対応するセットターとゲッターは\Magento\Customer\Api\Data\GroupExtensionInterface(このインタフェースは自動生成されています)
  2. に表示されます。
  3. \Magento\Customer\Api\GroupRepositoryInterface::save\Magento\Customer\Api\GroupRepositoryInterface::getById(および必要に応じて他のサービスメソッド)のプラグインを書き込み/ロードするためのプラグイン。拡張開発者として、この属性を格納する場所が任意のテーブルになる場合があります。例
  4. としての\Magento\Downloadable\Model\Plugin\AroundProductRepositorySave::aroundSaveを参照してください
  5. この属性をコレクションに表示する必要がある場合(検索可能/フィルタラブルにするため)、joinノードを宣言します。そうでなければ、この
  6. をスキップするだけです
  7. カスタム属性としてアクセスします.$customerGroup->getExtensionAttributes()->getMyAttribute()は、customerGroup\Magento\Customer\Api\Data\GroupInterfaceを実装します。 setMyAttribute()
  8. に使用できます 以下は、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帰属
所属していません magento.stackexchange
scroll top