configurable_attribute.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\Eav\Api\AttributeRepositoryInterface;
  9. $eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
  10. $attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable');
  11. $eavConfig->clear();
  12. /** @var $installer \Magento\Catalog\Setup\CategorySetup */
  13. $installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
  14. if (!$attribute->getId()) {
  15. /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
  16. $attribute = Bootstrap::getObjectManager()->create(
  17. \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
  18. );
  19. /** @var AttributeRepositoryInterface $attributeRepository */
  20. $attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
  21. $attribute->setData(
  22. [
  23. 'attribute_code' => 'test_configurable',
  24. 'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
  25. 'is_global' => 1,
  26. 'is_user_defined' => 1,
  27. 'frontend_input' => 'select',
  28. 'is_unique' => 0,
  29. 'is_required' => 0,
  30. 'is_searchable' => 0,
  31. 'is_visible_in_advanced_search' => 0,
  32. 'is_comparable' => 0,
  33. 'is_filterable' => 0,
  34. 'is_filterable_in_search' => 0,
  35. 'is_used_for_promo_rules' => 0,
  36. 'is_html_allowed_on_front' => 1,
  37. 'is_visible_on_front' => 0,
  38. 'used_in_product_listing' => 0,
  39. 'used_for_sort_by' => 0,
  40. 'frontend_label' => ['Test Configurable'],
  41. 'backend_type' => 'int',
  42. 'option' => [
  43. 'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']],
  44. 'order' => ['option_0' => 1, 'option_1' => 2],
  45. ],
  46. ]
  47. );
  48. $attributeRepository->save($attribute);
  49. /* Assign attribute to attribute set */
  50. $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
  51. }
  52. $eavConfig->clear();