0

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:

[Magento\Framework\Exception\LocalizedException]
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?

EDIT:

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"/>
</type>
  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);
        $group->setExtensionAttributes($extensionAttributes);

        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:

var_dump($group->__toArray());

And this is the result:

array(4) {
  ["code"]=>
  string(4) "test"
  ["tax_class_id"]=>
  int(3)
  ["tax_class_name"]=>
  string(6) "string"
  ["extension_attributes"]=>
  array(1) {
    ["gsp_code"]=>
    string(4) "test"
  }
}
array(4) {
  ["id"]=>
  string(1) "6"
  ["code"]=>
  string(4) "test"
  ["tax_class_id"]=>
  int(3)
  ["tax_class_name"]=>
  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.

1

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.

| improve this answer | |
  • 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. – slayerbleast Jun 27 '17 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 '17 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? – slayerbleast Jun 27 '17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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