I've already added some custom attributes into order and customer, now, I'm trying to do the same with CustomerGroups and it's not working. I'm failling on the first step, the setup script:

class InstallData implements InstallDataInterface

    private $customerSetupFactory;

    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
        $this->customerSetupFactory = $customerSetupFactory;

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute('customer_group', 'custom_code', [
            'type' => 'varchar'

When I run setup:upgrade it shows that:

Wrong entity ID

Why? I searched on module-customer and I found the model I want to attach the custom attributes: vendor/magento/module-customer/Model/Group.php and it has the constant:

const ENTITY = 'customer_group';

So why it's failing?

What I'm missing?


After @Sohel Rana response, I'm trying this way, but it's harder than I expect. I did the following steps:

  1. I added a column on customer_group table with InstallSchema.

  2. Defined the plugin for intercept the get and save methods:

<type name="Magento\Customer\Model\ResourceModel\GroupRepository">
    <plugin name="Gsp_CustomerCrm::GroupRepository" type="Gsp\CustomerCrm\Plugin\GroupRepository"/>
  1. Created the plugin, based with another plugin I have of order:

class GroupRepository

    protected $groupExtension;

    public function __construct(\Magento\Customer\Api\Data\GroupExtension $groupExtension)
        $this->groupExtension = $groupExtension;

    public function afterGetById(\Magento\Customer\Model\ResourceModel\GroupRepository $groupRepository, $group)
        $extensionAttributes = $group->getExtensionAttributes();
        if ($extensionAttributes === null) {
            $extensionAttributes = $this->groupExtension;
        $value = $group->getData('gsp_code') ? $group->getData('gsp_code') : null;
        $extensionAttributes->setData('gsp_code', $value);

        return $group;

But when I call getById method it shows that error:

Call to undefined method Magento\\Customer\\Model\\Data\\Group::getData()

I see that the Group object hasn't got getData method, getGspCode doesn't exists neither. so how I can retrieve my attribute?

The only way I see it's doing by a custom query. But I feel a bit messy this solution. Is there a proper way to get my attribute?

After that I tried to do the same with afterSave method, and I have the same problem.

I don't get what is going on. I created a beforeSave and afterSave

with this code:


And this is the result:

array(4) {
  string(4) "test"
  string(6) "string"
  array(1) {
    string(4) "test"
array(4) {
  string(1) "6"
  string(4) "test"
  string(15) "Retail Customer"

Why on beforeSave appears my attribute but on afterSave doesn't?

Seems that this should work without any plugin, just saving and retrieving by itself, but for some reason is not working.

2 Answers 2


You trying wrong. In magento, no 'curtomer_group' entity type. Open eav_entity_type table, you can find following entity_type

1   customer
2   customer_address
3   catalog_category
4   catalog_product
5   order       
6   invoice 
7   creditmemo  
8   shipment    

So you can create attribute for this type.

  • I understand I can't add a custom attribute into customer_group by this way. So how do I do it? I need to create customerGroups via API adding customAttributes. I've seen on the API doc, there are extension_attributes How can I add information there? I've already created the extension_attributes.xml and the POST call works setting my extension attributes, but it doesn't save it.
    – Marc Pont
    Jun 27, 2017 at 10:34
  • Customer group use simple flat table. So you can create/update/delete using API, or Repository. All are available under 'Customer' module.
    – Sohel Rana
    Jun 27, 2017 at 10:41
  • I have an extension that does this on the InstallData script: $this->quoteSetup->addAttribute('quote_address', 'payment_fee_amount', [ 'type'=>'decimal' ]); How is this possible if you say only can create attributes for this 8 entities? what happens with quote_address?
    – Marc Pont
    Jun 27, 2017 at 15:26

Ive tried to add as extension_attribute, but it looks I need to create aroundSave plugin for GroupRepositoryInterface. Original save() method only moves extension_attributes to target object, but didnt move custom det data from beforeSave plugin.

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.