add customer attributes via upgrad data in magento 2

Solutions on MaxInterview for add customer attributes via upgrad data in magento 2 by the best coders in the world

showing results for - "add customer attributes via upgrad data in magento 2"
Coline
31 Feb 2016
1<?php
2namespace MyCompany\Extendedcustomer\Setup;
3
4use Magento\Customer\Setup\CustomerSetupFactory;
5use Magento\Customer\Model\Customer;
6use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
7use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
8use Magento\Framework\Setup\UpgradeDataInterface;
9use Magento\Framework\Setup\ModuleContextInterface;
10use Magento\Framework\Setup\ModuleDataSetupInterface;
11
12class UpgradeData implements UpgradeDataInterface
13{
14    protected $customerSetupFactory;
15
16    private $attributeSetFactory;
17
18    public function __construct(
19        CustomerSetupFactory $customerSetupFactory,
20        AttributeSetFactory $attributeSetFactory
21    ) {
22        $this->customerSetupFactory = $customerSetupFactory;
23        $this->attributeSetFactory = $attributeSetFactory;
24    }
25
26
27    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
28    {
29        $setup->startSetup();
30        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
31
32        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
33        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
34
35        $attributeSet = $this->attributeSetFactory->create();
36        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
37        if (version_compare($context->getVersion(), '1.0.1') < 0) {
38            $customerSetup->addAttribute(
39                    \Magento\Customer\Model\Customer::ENTITY,
40                    'current_device',                    
41                    [
42                        'type' => 'text',
43                        'label' => 'Current Device',
44                        'input' => 'text',
45                        'required' => false,
46                        'visible' => true,
47                        'user_defined' => true,
48                        'sort_order' => 1000,
49                        'position' => 1000,
50                        'system' => 0,
51                    ]
52                );
53            $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_device')
54            ->addData([
55                'attribute_set_id' => 1,
56                'attribute_group_id' => 1,
57                'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
58            ]);
59
60            $Attribute->save();
61
62            $customerSetup->addAttribute(Customer::ENTITY, 'current_scandevice', [
63                'type' => 'varchar',
64                'label' => 'Current RF Scan Device',
65                'input' => 'text',
66                'required' => false,
67                'visible' => true,
68                'user_defined' => true,
69                'sort_order' => 1000,
70                'position' => 1000,
71                'system' => 0,
72            ]);
73
74            $Attribute2 = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'current_scandevice')
75            ->addData([
76                'attribute_set_id' => 1,
77                'attribute_group_id' => 1,
78                'used_in_forms' => ['adminhtml_customer','checkout_register', 'customer_account_create', 'customer_account_edit','adminhtml_checkout'],
79            ]);
80
81            $Attribute2->save();
82
83        }
84
85        $setup->endSetup();
86    }
87}
88