I want to add a custom attribute to my customer. For now I have this : (on magento 2.2.4)
<?php
namespace My\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory, \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "customer_bonus");
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_bonus', [
'type' => 'varchar',
'label' => 'Customer Bonus',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'system' => false,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_bonus')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms'=>['adminhtml_customer']
]);
$attribute->save();
$installer->endSetup();
}
}
But there's nothing on my backend, I want have something like this result : Magento 2 - How to add a custom column in customer grid
And CustomerSetupFactory is not find in PHP Storm. I tried
php bin/magento setup:di:compile
but nothing change.
Edit : the class is found with setup:di:compile dans after if I do setup:upgrade it's not found anymore.