0

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.

  • Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code. – Aditya Shah May 25 '18 at 15:39
  • they are an automatically generated class type. – Aditya Shah May 25 '18 at 15:39
  • Please try reindeing – Aditya Shah May 25 '18 at 15:39
  • php bin/magento indexer:reindex – Aditya Shah May 25 '18 at 15:39
  • Reidexer didn't change it, do you have another idea ? – Morgan Tartreau May 25 '18 at 15:48
0
class UpgradeData implements UpgradeDataInterface
 {
  /**
   * {@inheritdoc}
   * @SuppressWarnings(PHPMD.NPathComplexity)
   * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
    protected $customerSetupFactory;
    private $attributeSetFactory;

public function __construct(
    \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    /** @var CustomerSetup $customerSetup */

    if (version_compare($context->getVersion(), '1.0.1', '<')) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(Customer::ENTITY, 'attributename', [
            'type'      => 'int',
            'backend'   => '',
            'label'     => 'Label',
            'input'     => 'text',
            'source'    => '',
            'visible'   => true,
            'required'  => false,
            'default'   => '',
            'frontend'  => '',
            'sort_order' => 1000,
            'position' => 1000,
            'unique'    => true,
            'system' => 0,
        ] );

    //add attribute to attribute set
    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'sap_id')
    ->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer'],
    ]);

    $attribute->save();
    $setup->endSetup();
   }     
 }
}

This may help you out

  • Where does this class is suppose to go ? – Morgan Tartreau May 28 '18 at 7:28
  • The install data or update data of setup scripts is useful to add a new attribute to the ui forms. – Sairam Sigirisetty May 28 '18 at 10:43

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.