|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +use Magento\Catalog\Api\Data\ProductAttributeInterface; |
| 9 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 10 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; |
| 11 | +use Magento\Catalog\Setup\CategorySetup; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | + |
| 14 | +$objectManager = Bootstrap::getObjectManager(); |
| 15 | +/** @var $attribute Attribute */ |
| 16 | +$attribute = $objectManager->create(Attribute::class); |
| 17 | +/** @var ProductAttributeRepositoryInterface $attributeRepository */ |
| 18 | +$attributeRepository = $objectManager->create(ProductAttributeRepositoryInterface::class); |
| 19 | +/** @var $installer CategorySetup */ |
| 20 | +$installer = $objectManager->create(CategorySetup::class); |
| 21 | +$entityTypeId = $installer->getEntityTypeId(ProductAttributeInterface::ENTITY_TYPE_CODE); |
| 22 | + |
| 23 | +if (!$attribute->loadByCode($entityTypeId, 'dropdown_attribute')->getId()) { |
| 24 | + $attribute->setData( |
| 25 | + [ |
| 26 | + 'attribute_code' => 'dropdown_attribute', |
| 27 | + 'entity_type_id' => $entityTypeId, |
| 28 | + 'is_global' => 0, |
| 29 | + 'is_user_defined' => 1, |
| 30 | + 'frontend_input' => 'select', |
| 31 | + 'is_unique' => 0, |
| 32 | + 'is_required' => 0, |
| 33 | + 'is_searchable' => 0, |
| 34 | + 'is_visible_in_advanced_search' => 0, |
| 35 | + 'is_comparable' => 0, |
| 36 | + 'is_filterable' => 0, |
| 37 | + 'is_filterable_in_search' => 0, |
| 38 | + 'is_used_for_promo_rules' => 0, |
| 39 | + 'is_html_allowed_on_front' => 1, |
| 40 | + 'is_visible_on_front' => 1, |
| 41 | + 'used_in_product_listing' => 1, |
| 42 | + 'used_for_sort_by' => 0, |
| 43 | + 'frontend_label' => ['Drop-Down Attribute'], |
| 44 | + 'backend_type' => 'int', |
| 45 | + 'option' => [ |
| 46 | + 'value' => [ |
| 47 | + 'option_1' => ['Option 1'], |
| 48 | + 'option_2' => ['Option 2'], |
| 49 | + 'option_3' => ['Option 3'], |
| 50 | + ], |
| 51 | + 'order' => [ |
| 52 | + 'option_1' => 1, |
| 53 | + 'option_2' => 2, |
| 54 | + 'option_3' => 3, |
| 55 | + ], |
| 56 | + ], |
| 57 | + ] |
| 58 | + ); |
| 59 | + $attributeRepository->save($attribute); |
| 60 | + /* Assign attribute to attribute set */ |
| 61 | + $installer->addAttributeToGroup( |
| 62 | + ProductAttributeInterface::ENTITY_TYPE_CODE, |
| 63 | + 'Default', |
| 64 | + 'Attributes', |
| 65 | + $attribute->getId() |
| 66 | + ); |
| 67 | +} |
0 commit comments