Skip to main content
added 238 characters in body
Source Link

InstallData Vendor/Module/Setup/InstallData.phpInstallData Vendor/Module/Setup/InstallData.php

<?php
 
    namespace Vendor\Module\Setup;
    
    use Magento\Customer\Setup\CustomerSetupFactory;
    use Magento\Customer\Model\Customer;
    use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
    use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;

 

    class InstallData implements InstallDataInterface
    {
        
    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;
    
    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;
    
    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

 
    
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    
     /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        
        $customerSetup->addAttribute(Customer::ENTITY, 'testfield', [
            'type' => 'varchar',
            'label' => 'Test Field',
            'input' => 'select',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'visible_on_front' => true, 
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'source' => \Vendor\Module\Model\Customer\Attribute\Source\Test,
        ]);
        
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'testfield')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'],
        ]);
        
        $attribute->save();
      
  }
}

}

Source Model (Needed Code) : Vendor\Module\Model\Customer\Attribute\Source\TestVendor\Module\Model\Customer\Attribute\Source\Test

InstallData Vendor/Module/Setup/InstallData.php

<?php
 
namespace Vendor\Module\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

 

class InstallData implements InstallDataInterface
{
    
/**
 * @var CustomerSetupFactory
 */
protected $customerSetupFactory;

/**
 * @var AttributeSetFactory
 */
private $attributeSetFactory;

/**
 * @param CustomerSetupFactory $customerSetupFactory
 * @param AttributeSetFactory $attributeSetFactory
 */
public function __construct(
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}

 

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    
     /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
    $customerSetup->addAttribute(Customer::ENTITY, 'testfield', [
        'type' => 'varchar',
        'label' => 'Test Field',
        'input' => 'select',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'visible_on_front' => true, 
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'source' => \Vendor\Module\Model\Customer\Attribute\Source\Test,
    ]);
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'testfield')
    ->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'],
    ]);
    
    $attribute->save();
      
  
}

}

Source Model (Needed Code) : Vendor\Module\Model\Customer\Attribute\Source\Test

InstallData Vendor/Module/Setup/InstallData.php

<?php
    namespace Vendor\Module\Setup;
    
    use Magento\Customer\Setup\CustomerSetupFactory;
    use Magento\Customer\Model\Customer;
    use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
    use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;

    class InstallData implements InstallDataInterface
    {
        
    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;
    
    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;
    
    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }
    
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        
        $customerSetup->addAttribute(Customer::ENTITY, 'testfield', [
            'type' => 'varchar',
            'label' => 'Test Field',
            'input' => 'select',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'visible_on_front' => true, 
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'source' => \Vendor\Module\Model\Customer\Attribute\Source\Test,
        ]);
        
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'testfield')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'],
        ]);
        
        $attribute->save();
    }
}

Source Model (Needed Code) : Vendor\Module\Model\Customer\Attribute\Source\Test

Source Link
MageD
  • 107
  • 2
  • 12

Magento 2 Add Custom Select to Customer : Source Model

I added an attribute to customer entity

My goal is to have this attribute as Select (with data from another db table), but i didn't find any tuto about the source/model that i should create !!

Here is the Files :

InstallData Vendor/Module/Setup/InstallData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;



class InstallData implements InstallDataInterface
{
    
/**
 * @var CustomerSetupFactory
 */
protected $customerSetupFactory;

/**
 * @var AttributeSetFactory
 */
private $attributeSetFactory;

/**
 * @param CustomerSetupFactory $customerSetupFactory
 * @param AttributeSetFactory $attributeSetFactory
 */
public function __construct(
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}



public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
    $customerSetup->addAttribute(Customer::ENTITY, 'testfield', [
        'type' => 'varchar',
        'label' => 'Test Field',
        'input' => 'select',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'visible_on_front' => true, 
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'source' => \Vendor\Module\Model\Customer\Attribute\Source\Test,
    ]);
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'testfield')
    ->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'],
    ]);
    
    $attribute->save();
      
  
}

}

Source Model (Needed Code) : Vendor\Module\Model\Customer\Attribute\Source\Test

<?php
namespace Vendor\Module\Model\Customer\Attribute\Source;


class Test extends \Magento\Eav\Model\Entity\Attribute\Source\Table
{
    
    public function __construct(..........) 
{
        ......................
    }

    /**
     * @return array
     */
    public function getAllOptions()
    {
        ...................

        return $this->_options;
    }

    
}

Could any one help me how to create this source model.