· 7 years ago · Feb 26, 2019, 11:06 AM
1public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
2{
3
4 /** @var CustomerSetup $customerSetup */
5 $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
6
7 //install customer group attributes
8 $setup->startSetup();
9 if (version_compare($context->getVersion(), '1.0.1', '<')) {
10 // insert default customer groups
11 $setup->getConnection()->insert(
12 $setup->getTable('customer_group'),
13 ['customer_group_id' => 4, 'customer_group_code' => 'University / Research Lab Customers', 'tax_class_id' => 3]
14 );
15 $setup->getConnection()->insert(
16 $setup->getTable('customer_group'),
17 ['customer_group_id' => 5, 'customer_group_code' => 'Hospitals / Clinical Centers Customers', 'tax_class_id' => 3]
18 );
19 $setup->getConnection()->insert(
20 $setup->getTable('customer_group'),
21 ['customer_group_id' => 6, 'customer_group_code' => 'GMP – Pharma / Biotech Customers', 'tax_class_id' => 3]
22 );
23
24 $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
25 $attributeSetId = $customerEntity->getDefaultAttributeSetId();
26
27 /** @var $attributeSet AttributeSet */
28 $attributeSet = $this->attributeSetFactory->create();
29 $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
30
31
32 $attributesInfo = [
33 'contact_number' => [
34 'label' => 'Contact Number',
35 'type' => 'varchar',
36 'input' => 'text',
37 'input_validation' => 'numeric',
38 'min_text_length' => '0',
39 'max_text_length' => '10',
40 'is_used_in_grid' => true,
41 'is_visible_in_grid' => false,
42 'is_filterable_in_grid' => true,
43 'is_searchable_in_grid' => true,
44 'is_visible' => true,
45 'is_required' => false,
46 'sort_order' => '999',
47 'system' => 0,
48 ],
49 'customer_company' => [
50 'label' => 'Customer Company',
51 'type' => 'varchar',
52 'input' => 'text',
53 'is_used_in_grid' => true,
54 'is_visible_in_grid' => false,
55 'is_filterable_in_grid' => true,
56 'is_searchable_in_grid' => true,
57 'is_visible' => true,
58 'is_required' => false,
59 'sort_order' => '1000',
60 'system' => 0,
61 ]
62 ];
63
64 foreach ($attributesInfo as $attributeCode => $attributeParams) {
65 $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
66 $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $attributeCode)
67 ->addData([
68 'attribute_set_id' => $attributeSetId,
69 'attribute_group_id' => $attributeGroupId,
70 'used_in_forms' => ['customer_account_create','customer_account_edit','adminhtml_customer']
71 ]);
72 $attribute->save();
73 }
74 unset($attributesInfo);
75 }
76 //create customer address attribute
77 $attributesInfo = [
78 'room_no' => [
79 'label' => 'Room No',
80 'type' => 'varchar',
81 'input' => 'text',
82 'is_used_in_grid' => true,
83 'is_visible_in_grid' => false,
84 'is_filterable_in_grid' => true,
85 'is_searchable_in_grid' => true,
86 'is_user_defined' => true,
87 'is_visible' => true,
88 'is_required' => false,
89 'sort_order' => '999',
90 'system' => 0,
91 ],
92 'building' => [
93 'label' => 'Building',
94 'type' => 'varchar',
95 'input' => 'text',
96 'is_used_in_grid' => true,
97 'is_visible_in_grid' => false,
98 'is_filterable_in_grid' => true,
99 'is_searchable_in_grid' => true,
100 'is_visible' => true,
101 'is_required' => false,
102 'is_user_defined' => true,
103 'sort_order' => '1000',
104 'system' => 0,
105 ]
106 ];
107
108 foreach ($attributesInfo as $attributeCode => $attributeParams) {
109 $customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
110 }
111 $setup->endSetup();
112}
113
114php bin/magento indexer:reindex
115
116Customer Grid indexer process unknown error:
117SQLSTATE[42000]: Syntax error or access violation: 1070 Too many key parts specified; max 16 parts allowed, query was: CREATE TABLE IF NOT EXISTS `customer_grid_flat` (
118 `entity_id` int UNSIGNED NOT NULL COMMENT 'Entity ID' ,
119 `name` text NULL COMMENT 'Name' ,
120 `email` varchar(255) NULL COMMENT 'Email' ,
121 `group_id` int NULL COMMENT 'Group_id' ,
122 `created_at` timestamp NULL default NULL COMMENT 'Created_at' ,
123 `website_id` int NULL COMMENT 'Website_id' ,
124 `confirmation` varchar(255) NULL COMMENT 'Confirmation' ,
125 `created_in` text NULL COMMENT 'Created_in' ,
126 `dob` date NULL COMMENT 'Dob' ,
127 `gender` int NULL COMMENT 'Gender' ,
128 `taxvat` varchar(255) NULL COMMENT 'Taxvat' ,
129 `lock_expires` timestamp NULL default NULL COMMENT 'Lock_expires' ,
130 `contact_number` varchar(255) NULL COMMENT 'Contact_number' ,
131 `customer_company` varchar(255) NULL COMMENT 'Customer_company' ,
132 `shipping_full` text NULL COMMENT 'Shipping_full' ,
133 `billing_full` text NULL COMMENT 'Billing_full' ,
134 `billing_firstname` varchar(255) NULL COMMENT 'Billing_firstname' ,
135 `billing_lastname` varchar(255) NULL COMMENT 'Billing_lastname' ,
136 `billing_telephone` varchar(255) NULL COMMENT 'Billing_telephone' ,
137 `billing_postcode` varchar(255) NULL COMMENT 'Billing_postcode' ,
138 `billing_country_id` varchar(255) NULL COMMENT 'Billing_country_id' ,
139 `billing_region` varchar(255) NULL COMMENT 'Billing_region' ,
140 `billing_street` varchar(255) NULL COMMENT 'Billing_street' ,
141 `billing_city` varchar(255) NULL COMMENT 'Billing_city' ,
142 `billing_fax` varchar(255) NULL COMMENT 'Billing_fax' ,
143 `billing_vat_id` varchar(255) NULL COMMENT 'Billing_vat_id' ,
144 `billing_company` varchar(255) NULL COMMENT 'Billing_company' ,
145 `billing_building` varchar(255) NULL COMMENT 'Billing_building' ,
146 `billing_room_no` varchar(255) NULL COMMENT 'Billing_room_no' ,
147 PRIMARY KEY (`entity_id`),
148 INDEX `CUSTOMER_GRID_FLAT_GROUP_ID` (`group_id`),
149 INDEX `CUSTOMER_GRID_FLAT_CREATED_AT` (`created_at`),
150 INDEX `CUSTOMER_GRID_FLAT_WEBSITE_ID` (`website_id`),
151 INDEX `CUSTOMER_GRID_FLAT_CONFIRMATION` (`confirmation`),
152 INDEX `CUSTOMER_GRID_FLAT_DOB` (`dob`),
153 INDEX `CUSTOMER_GRID_FLAT_GENDER` (`gender`),
154 INDEX `CUSTOMER_GRID_FLAT_BILLING_COUNTRY_ID` (`billing_country_id`),
155 FULLTEXT `FTI_5DD806E7D7383F9D4376BB41BEB968FC` (`name`, `email`, `created_in`, `taxvat`, `contact_number`, `customer_company`, `shipping_full`, `billing_full`, `billing_firstname`, `billing_lastname`, `billing_telephone`, `billing_postcode`, `billing_region`, `billing_city`, `billing_fax`, `billing_company`, `billing_building`, `billing_room_no`)
156) COMMENT='customer_grid_flat' ENGINE=INNODB charset=utf8 COLLATE=utf8_general_ci