5

I have to add one drop-down field with two options. 1. Enable and 2. Disable. in the customer in admin side,

My code as following:

<?php

namespace Test\CustomerTax\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface {

    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY, 'customer_tax', [
            'type' => 'int',
            'label' => 'Apply Customer Tax',
            'input' => 'select',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required' => true,
            'default' => '0',
            'sort_order' => 100,
            'system' => false,
            'position' => 100
                ]
        );
        $sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_tax');
        $sampleAttribute->setData(
                'used_in_forms', ['adminhtml_customer']
        );
        $sampleAttribute->save();
        }
    }

I am getting an error as follows:

Missing required argument $options of Magento\Eav\Model\Entity\Attribute\Source\Config.

Please give me some solution.

Magecode
  • 1,504
  • 2
  • 18
  • 43
  • I have tested your script in Magento 2.2 its working fine and create attribute for customer. – Yogesh Nov 11 '17 at 13:30

1 Answers1

2

Try this code to add customer attribute. I hope this work for you.

<?php

namespace Test\CustomerTax\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 {

protected $customerSetupFactory;
private $_attributeSetFactory;

public function __construct(
CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $_attributeSetFactory
) {
    $this->customerSetupFactory = $customerSetupFactory;
    $this->_attributeSetFactory = $_attributeSetFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();


    $attributeSet = $this->_attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(Customer::ENTITY, 'customer_tax', [
        'type' => 'int',
        'label' => 'Apply Customer Tax',
        'input' => 'select',
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'sort_order' => 1000,
        'position' => 1000,
        'system' => 0,
        'is_used_in_grid' => true,
        'is_visible_in_grid' => true,
        'is_filterable_in_grid' => true,
        'is_searchable_in_grid' => true
    ]);


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

        $attribute->save();
    }

}