|
| 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\ProductRepositoryInterface; |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | +use Magento\Eav\Api\AttributeRepositoryInterface; |
| 12 | +use Magento\Eav\Model\Config as EavConfig; |
| 13 | +use Magento\Catalog\Setup\CategorySetup; |
| 14 | +use Magento\Catalog\Model\ResourceModel\Eav\Attribute as EavAttribute; |
| 15 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 16 | +use Magento\Catalog\Model\Product\Type as ProductType; |
| 17 | +use Magento\Catalog\Model\Product\Visibility; |
| 18 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 19 | +use Magento\Eav\Api\AttributeOptionManagementInterface; |
| 20 | +use Magento\Eav\Api\Data\AttributeOptionInterface; |
| 21 | +use Magento\Store\Model\StoreManagerInterface; |
| 22 | +use Magento\Store\Api\Data\StoreInterface; |
| 23 | + |
| 24 | +$objectManager = Bootstrap::getObjectManager(); |
| 25 | +$storeManager = $objectManager->get(StoreManagerInterface::class); |
| 26 | +/** @var StoreInterface $store */ |
| 27 | +$store = $storeManager->getStore(); |
| 28 | +$eavConfig = $objectManager->get(EavConfig::class); |
| 29 | +$eavConfig->clear(); |
| 30 | +$attribute = $eavConfig->getAttribute(ProductAttributeInterface::ENTITY_TYPE_CODE, 'dropdown_without_default'); |
| 31 | +/** @var CategorySetup $installer */ |
| 32 | +$installer = $objectManager->get(CategorySetup::class); |
| 33 | +$attributeSetId = $installer->getAttributeSetId(ProductAttributeInterface::ENTITY_TYPE_CODE, 'Default'); |
| 34 | + |
| 35 | +/** @var ProductInterface $product */ |
| 36 | +$product = $objectManager->get(ProductInterface::class); |
| 37 | +$product->setTypeId(ProductType::TYPE_SIMPLE) |
| 38 | + ->setAttributeSetId($attributeSetId) |
| 39 | + ->setName('Simple Product1') |
| 40 | + ->setSku('test_attribute_dropdown_without_default') |
| 41 | + ->setPrice(10) |
| 42 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 43 | + ->setStatus(Status::STATUS_ENABLED); |
| 44 | +/** @var ProductRepositoryInterface $productRepository */ |
| 45 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 46 | +$product = $productRepository->save($product); |
| 47 | + |
| 48 | +if (!$attribute->getId()) { |
| 49 | + /** @var $attribute */ |
| 50 | + $attribute = $objectManager->get(EavAttribute::class); |
| 51 | + /** @var AttributeRepositoryInterface $attributeRepository */ |
| 52 | + $attributeRepository = $objectManager->get(AttributeRepositoryInterface::class); |
| 53 | + $attribute->setData( |
| 54 | + [ |
| 55 | + 'attribute_code' => 'dropdown_without_default', |
| 56 | + 'entity_type_id' => $installer->getEntityTypeId(ProductAttributeInterface::ENTITY_TYPE_CODE), |
| 57 | + 'is_global' => 0, |
| 58 | + 'is_user_defined' => 1, |
| 59 | + 'frontend_input' => 'select', |
| 60 | + 'is_unique' => 0, |
| 61 | + 'is_required' => 0, |
| 62 | + 'is_searchable' => 1, |
| 63 | + 'is_visible_in_advanced_search' => 1, |
| 64 | + 'is_comparable' => 1, |
| 65 | + 'is_filterable' => 1, |
| 66 | + 'is_filterable_in_search' => 1, |
| 67 | + 'is_used_for_promo_rules' => 0, |
| 68 | + 'is_html_allowed_on_front' => 1, |
| 69 | + 'is_visible_on_front' => 1, |
| 70 | + 'used_in_product_listing' => 1, |
| 71 | + 'used_for_sort_by' => 1, |
| 72 | + 'frontend_label' => ['Test Configurable'], |
| 73 | + 'backend_type' => 'int', |
| 74 | + 'option' => [ |
| 75 | + 'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']], |
| 76 | + 'order' => ['option_0' => 1, 'option_1' => 2], |
| 77 | + ], |
| 78 | + ] |
| 79 | + ); |
| 80 | + $attributeRepository->save($attribute); |
| 81 | + /* Assign attribute to attribute set */ |
| 82 | + $installer->addAttributeToGroup( |
| 83 | + ProductAttributeInterface::ENTITY_TYPE_CODE, |
| 84 | + 'Default', |
| 85 | + 'General', |
| 86 | + $attribute->getId() |
| 87 | + ); |
| 88 | +} |
| 89 | +/** @var AttributeOptionManagementInterface $options */ |
| 90 | +$attributeOption = $objectManager->get(AttributeOptionManagementInterface::class); |
| 91 | +/* Getting the first nonempty option */ |
| 92 | +/** @var AttributeOptionInterface $option */ |
| 93 | +$option = $attributeOption->getItems($attribute->getEntityTypeId(), $attribute->getAttributeCode())[1]; |
| 94 | +$product->setStoreId($store->getId()) |
| 95 | + ->setData('dropdown_without_default', $option->getValue()); |
| 96 | +$productRepository->save($product); |
0 commit comments