I've added some custom fields to the form of customer group by using upgradeSchema.php.

After that I found that the original fields like customer group code and tax id are saved by using setter methods in the API provided. It is completely different from Magento 1.X that just use setXXX() to save.

  • I've tried using \Magento\Customer\Api\Data\GroupInterface $customerGroup->setData('program_type',$programType); program_type is corresponding to the table column 'program_type' to save into database, but failed.
    – Ricky.C
    Nov 30, 2015 at 5:29
  • Should I write a custom API with getter and setter to save the fields?
    – Ricky.C
    Nov 30, 2015 at 5:38

2 Answers 2


Extension attributes mechanism should be used in this case. It allows extension of core APIs by 3rd party modules. Generic steps for enabling new extension attribute:

  1. Declare extension attribute as described in the official docs. After clearing var and running <project_root>/bin/magento setup:di:compile, corresponding setter and getter for this new attribute should appear in \Magento\Customer\Api\Data\GroupExtensionInterface (this interface is auto-generated)
  2. Write plugins for \Magento\Customer\Api\GroupRepositoryInterface::save, \Magento\Customer\Api\GroupRepositoryInterface::getById (and any other service methods as necessary) to save/load new attribute. As an extension developer, only you know where this attribute should be stored, may be any table. See \Magento\Downloadable\Model\Plugin\AroundProductRepositorySave::aroundSave as an example
  3. If you need to make this attribute visible in collection (to make it searchable/filterable), declare join node. If not then just skip this
  4. Access your custom attribute as: $customerGroup->getExtensionAttributes()->getMyAttribute(), where customerGroup implements \Magento\Customer\Api\Data\GroupInterface. setMyAttribute() can be used as well

Below is the example of configuration which should be put to VendorName/ModuleName/etc/extension_attributes.xml

<?xml version="1.0"?>
    <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">
                <resource ref="VendorName_ModuleName::someAclNode"/>
            <!--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">
  • I tried to add the extension_attributes.xml, but no new interface is generated. p.s I've deleted the generation folder and invoked some operation.....
    – Ricky.C
    Dec 1, 2015 at 5:41
  • My extension_attribute.xml: <?xml version="1.0"?> <config> <extension_attributes for="Magento\Customer\Api\Data\GroupInterface"> <attribute code="group_domain" type="string"/> </extension_attributes> </config>
    – Ricky.C
    Dec 1, 2015 at 6:03
  • File should be called extension_attributes.xml (plural). Try to invoke generation of all autogenerated entities using CLI. Dec 1, 2015 at 7:35
  • sorry for the typo on the above comment, the file I have actually is extension_attributes.xml
    – Ricky.C
    Dec 1, 2015 at 7:37
  • I've googled but found nothing. Can you let me know which command should be used? I'm a new comer that not familiar to the cli. Thanks.
    – Ricky.C
    Dec 1, 2015 at 8:33

Don't forget that a module needs a register.php file in it, and you must use bin/magento module:enable VendorName_ModuleName before it will show up!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.