Magento 2.3 - Add custom customer entity attribute with service contract
I found the solution by dump the ResourceModel classname used by the save()
method that was deprecated in favor of service contract. The resource model that I should used is Magento\Customer\Model\ResourceModel\Attribute
. Here the complete code:
<?php
namespace Ped\Referral\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResourceModel;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class AddCustomerReferralCodeAttribute implements DataPatchInterface
{
private const REFERRAL_CODE_FIELD_NAME = 'referral_code';
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @var AttributeResourceModel
*/
private $attributeResourceModel;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
EavSetupFactory $eavSetupFactory,
AttributeResourceModel $attributeResourceModel
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
$this->eavSetupFactory = $eavSetupFactory;
$this->attributeResourceModel = $attributeResourceModel;
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function apply()
{
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(
Customer::ENTITY,
static::REFERRAL_CODE_FIELD_NAME,
[
'type' => 'varchar',
'label' => 'Referral Code',
'input' => 'text',
'required' => false,
'sort_order' => 999,
'position' => 999,
'visible' => true,
'default' => '',
'user_defined' => true,
'system' => 0,
'unique' => true,
]
);
$referralCodeAttribute = $customerSetup->getEavConfig()
->getAttribute(Customer::ENTITY, static::REFERRAL_CODE_FIELD_NAME)
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
]);
$this->attributeResourceModel->save($referralCodeAttribute);
}
}