how to add multile attributes using upgradeschema in magento 2

Solutions on MaxInterview for how to add multile attributes using upgradeschema in magento 2 by the best coders in the world

showing results for - "how to add multile attributes using upgradeschema in magento 2"
Davide
02 Mar 2016
1<?php 
2
3namespace MyCompany\Extendedcustomer\Setup;
4
5use Magento\Framework\Setup\ModuleContextInterface;
6use Magento\Customer\Setup\CustomerSetupFactory;
7use Magento\Framework\Setup\ModuleDataSetupInterface;
8use Magento\Framework\Setup\UpgradeSchemaInterface;
9
10class UpgradeSchema implements UpgradeSchemaInterface
11{
12
13
14protected $customerSetupFactory;
15
16public function upgrade(
17    ModuleDataSetupInterface $setup, 
18    ModuleContextInterface $context
19) {
20    $setup->startSetup();
21
22    if (version_compare($context->getVersion(), '1.0.1') < 0) {
23
24        $setup->startSetup();
25
26        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
27
28        $customerSetup->addAttribute(
29                \Magento\Customer\Model\Customer::ENTITY,
30                'current_device',                    
31                [
32                    'type' => 'text',
33                    'label' => 'Current Device',
34                    'input' => 'text',
35                    'required' => false,
36                    'visible' => true,
37                    'user_defined' => true,
38                    'sort_order' => 300,
39                    'position' => 400,
40                    'default' => '',
41                    'system' => false,
42                    'is_used_in_grid' => 0,
43                    'is_visible_in_grid' => 1,
44                    'is_filterable_in_grid' => 1,
45                    'is_searchable_in_grid' => 1,
46                ]
47            );
48        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'current_device');
49        $Attribute->setData(
50        'used_in_forms',
51            [
52                'adminhtml_customer',
53                'customer_account_edit',
54                'adminhtml_checkout',
55                'adminhtml_customer_address',
56                'customer_address_edit',
57                'customer_register_address',
58            ]
59        );
60        $Attribute->save();
61
62        $customerSetup->addAttribute(
63                \Magento\Customer\Model\Customer::ENTITY,
64                'current_scandevice',                    
65                [
66                    'type' => 'text',
67                    'label' => 'Current RF Scan Device',
68                    'input' => 'text',
69                    'required' => false,
70                    'visible' => true,
71                    'user_defined' => true,
72                    'sort_order' => 300,
73                    'position' => 400,
74                    'default' => '',
75                    'system' => false,
76                    'is_used_in_grid' => 0,
77                    'is_visible_in_grid' => 1,
78                    'is_filterable_in_grid' => 1,
79                    'is_searchable_in_grid' => 1,
80                ]
81            );
82        $Attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'current_scandevice');
83        $Attribute->setData(
84        'used_in_forms',
85            [
86                'adminhtml_customer',
87                'customer_account_edit',
88                'adminhtml_checkout',
89                'adminhtml_customer_address',
90                'customer_address_edit',
91                'customer_register_address',
92            ]
93        );
94        $Attribute->save();
95
96        $setup->endSetup();
97
98     }
99
100   }
101}
102