Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
attribute_for_search.php
Go to the documentation of this file.
1 <?php
8 
10 $entityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
11  ->loadByCode('order');
12 $data = $entityType->getData();
13 $data['entity_type_code'] = 'test';
14 unset($data['entity_type_id']);
16 $testEntityType = $objectManager->create(\Magento\Eav\Model\Entity\Type::class)
17  ->setData($data)
18  ->save();
20 
22 $attributeSet = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
23 $attributeSet->setData([
24  'attribute_set_name' => 'test_attribute_set',
25  'entity_type_id' => $entityTypeId,
26  'sort_order' => 100,
27 ]);
28 $attributeSet->validate();
29 $attributeSet->save();
30 
32 $attributeGroup = $objectManager->create(\Magento\Eav\Model\Entity\Attribute\Group::class);
33 $attributeGroup->setData(
34  [
35  'attribute_set_id' => $attributeSet->getAttributeSetId(),
36  'sort_order' => 30,
37  'attribute_group_code' => 'test_attribute_group',
38  'default_id' => 0,
39  ]
40 );
41 $attributeGroup->save();
42 
44  [
45  'attribute_code' => 'attribute_for_search_1',
46  'entity_type_id' => $entityTypeId,
47  'backend_type' => 'varchar',
48  'is_required' => 1,
49  'is_user_defined' => 1,
50  'is_unique' => 0,
51  'attribute_set_id' => $testEntityType->getDefaultAttributeSetId(),
52  'attribute_group_id' => $attributeSet->getDefaultGroupId($testEntityType->getDefaultAttributeSetId()),
53  ],
54  [
55  'attribute_code' => 'attribute_for_search_2',
56  'entity_type_id' => $entityTypeId,
57  'backend_type' => 'varchar',
58  'is_required' => 1,
59  'is_user_defined' => 1,
60  'is_unique' => 0,
61  'attribute_set_id' => $testEntityType->getDefaultAttributeSetId(),
62  'attribute_group_id' => $attributeSet->getDefaultGroupId($testEntityType->getDefaultAttributeSetId()),
63  ],
64  [
65  'attribute_code' => 'attribute_for_search_3',
66  'entity_type_id' => $entityTypeId,
67  'backend_type' => 'varchar',
68  'is_required' => 1,
69  'is_user_defined' => 1,
70  'is_unique' => 0,
71  'attribute_set_id' => $attributeSet->getAttributeSetId(),
72  'attribute_group_id' => $attributeGroup->getAttributeGroupId(),
73  ],
74  [
75  'attribute_code' => 'attribute_for_search_4',
76  'entity_type_id' => $entityTypeId,
77  'backend_type' => 'int',
78  'is_required' => 0,
79  'is_user_defined' => 1,
80  'is_unique' => 0,
81  ],
82  [
83  'attribute_code' => 'attribute_for_search_5',
84  'entity_type_id' => $entityTypeId,
85  'backend_type' => 'varchar',
86  'is_required' => 0,
87  'is_user_defined' => 1,
88  'is_unique' => 0,
89  ],
90 ];
91 
92 foreach ($attributeData as $data) {
94  $attribute = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
95  $attribute->setData($data);
96  $attribute->save();
97 }