Magento2: How to add custom attributes into CustomerGroups
-
10-10-2020 - |
Question
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:
I added a column on
customer_group
table withInstallSchema
.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>
- 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.
Solution
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.