Magento 2カスタムフィールドを顧客グループの形式で保存する方法は?
-
13-12-2019 - |
質問
upgradeSchema.php
を使用して、カスタムグループの形式にカスタムフィールドを追加しました。
その後、私は顧客グループコードや税IDのような元のフィールドが提供されたAPIのSetterメソッドを使用して保存されていることがわかりました。SetXXX()を使用して保存するだけのMagento 1.xとはまったく異なります。
解決
拡張子属性メカニズムを使用する必要があります。これにより、サードパーティモジュールによるコアAPIを拡張できます。新しい拡張子属性を有効にするための一般的な手順:
- 公式ドキュメントに記載されているように拡張属性を宣言します。
var
をクリアして<project_root>/bin/magento setup:di:compile
を実行した後、この新しい属性の対応するセットターとゲッターは\Magento\Customer\Api\Data\GroupExtensionInterface
(このインタフェースは自動生成されています) に表示されます。
-
\Magento\Customer\Api\GroupRepositoryInterface::save
、\Magento\Customer\Api\GroupRepositoryInterface::getById
(および必要に応じて他のサービスメソッド)のプラグインを書き込み/ロードするためのプラグイン。拡張開発者として、この属性を格納する場所が任意のテーブルになる場合があります。例 としての - この属性をコレクションに表示する必要がある場合(検索可能/フィルタラブルにするため)、
join
ノードを宣言します。そうでなければ、この をスキップするだけです
- カスタム属性としてアクセスします.
$customerGroup->getExtensionAttributes()->getMyAttribute()
は、customerGroup
は\Magento\Customer\Api\Data\GroupInterface
を実装します。setMyAttribute()
は に使用できます
以下は、
\Magento\Downloadable\Model\Plugin\AroundProductRepositorySave::aroundSave
を参照してください
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
を使用する必要があります。
所属していません magento.stackexchange