|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class); |
8 |
| -$model->setName( |
9 |
| - 'custom_attribute1' |
10 |
| -)->setEntityTypeId( |
11 |
| - 2 |
12 |
| -)->setAttributeSetId( |
13 |
| - 2 |
14 |
| -)->setAttributeGroupId( |
15 |
| - 1 |
16 |
| -)->setFrontendInput( |
17 |
| - 'text' |
18 |
| -)->setFrontendLabel( |
19 |
| - 'custom_attribute_frontend_label' |
20 |
| -)->setIsUserDefined( |
21 |
| - 1 |
22 |
| -); |
23 |
| -$model->save(); |
24 |
| - |
25 |
| -$model2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class); |
26 |
| -$model2->setName( |
27 |
| - 'custom_attribute2' |
28 |
| -)->setEntityTypeId( |
29 |
| - 2 |
30 |
| -)->setAttributeSetId( |
31 |
| - 2 |
32 |
| -)->setAttributeGroupId( |
33 |
| - 1 |
34 |
| -)->setFrontendInput( |
35 |
| - 'text' |
36 |
| -)->setFrontendLabel( |
37 |
| - 'custom_attribute_frontend_label' |
38 |
| -)->setIsUserDefined( |
39 |
| - 1 |
40 |
| -); |
41 |
| -$model2->save(); |
| 7 | +/** @var \Magento\Framework\ObjectManagerInterface $objectManager */ |
| 8 | +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 9 | + |
| 10 | +/** @var \Magento\Customer\Model\AttributeFactory $attributeFactory */ |
| 11 | +$attributeFactory = $objectManager->create(\Magento\Customer\Model\AttributeFactory::class); |
| 12 | + |
| 13 | +/** @var \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository */ |
| 14 | +$attributeRepository = $objectManager->create(\Magento\Eav\Api\AttributeRepositoryInterface::class); |
42 | 15 |
|
43 | 16 | /** @var \Magento\Customer\Setup\CustomerSetup $setupResource */
|
44 |
| -$setupResource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
45 |
| - \Magento\Customer\Setup\CustomerSetup::class |
46 |
| -); |
47 |
| - |
48 |
| -$data = [['form_code' => 'customer_address_edit', 'attribute_id' => $model->getAttributeId()]]; |
49 |
| -$setupResource->getSetup()->getConnection()->insertMultiple( |
50 |
| - $setupResource->getSetup()->getTable('customer_form_attribute'), |
51 |
| - $data |
52 |
| -); |
53 |
| - |
54 |
| -$data2 = [['form_code' => 'customer_address_edit', 'attribute_id' => $model2->getAttributeId()]]; |
55 |
| -$setupResource->getSetup()->getConnection()->insertMultiple( |
56 |
| - $setupResource->getSetup()->getTable('customer_form_attribute'), |
57 |
| - $data2 |
58 |
| -); |
| 17 | +$setupResource = $objectManager->create(\Magento\Customer\Setup\CustomerSetup::class); |
| 18 | + |
| 19 | +$attributeNames = ['custom_attribute1', 'custom_attribute2']; |
| 20 | +foreach ($attributeNames as $attributeName) { |
| 21 | + /** @var \Magento\Customer\Model\Attribute $attribute */ |
| 22 | + $attribute = $attributeFactory->create(); |
| 23 | + |
| 24 | + $attribute->setName($attributeName) |
| 25 | + ->setEntityTypeId(2) |
| 26 | + ->setAttributeSetId(2) |
| 27 | + ->setAttributeGroupId(1) |
| 28 | + ->setFrontendInput('text') |
| 29 | + ->setFrontendLabel('custom_attribute_frontend_label') |
| 30 | + ->setIsUserDefined(true); |
| 31 | + |
| 32 | + $attributeRepository->save($attribute); |
| 33 | + |
| 34 | + $setupResource->getSetup() |
| 35 | + ->getConnection() |
| 36 | + ->insertMultiple( |
| 37 | + $setupResource->getSetup()->getTable('customer_form_attribute'), |
| 38 | + [['form_code' => 'customer_address_edit', 'attribute_id' => $attribute->getAttributeId()]] |
| 39 | + ); |
| 40 | +} |
0 commit comments