select_attribute.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. use Magento\TestFramework\Helper\Bootstrap;
  7. $objectManager = Bootstrap::getObjectManager();
  8. /** @var \Magento\Catalog\Setup\CategorySetup $installer */
  9. $installer = $objectManager->create(\Magento\Catalog\Setup\CategorySetup::class);
  10. /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
  11. $selectAttribute = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
  12. $selectAttribute->setData(
  13. [
  14. 'attribute_code' => 'select_attribute',
  15. 'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
  16. 'is_global' => 1,
  17. 'is_user_defined' => 1,
  18. 'frontend_input' => 'select',
  19. 'is_unique' => 0,
  20. 'is_required' => 0,
  21. 'is_searchable' => 1,
  22. 'is_visible_in_advanced_search' => 0,
  23. 'is_comparable' => 0,
  24. 'is_filterable' => 1,
  25. 'is_filterable_in_search' => 0,
  26. 'is_used_for_promo_rules' => 0,
  27. 'is_html_allowed_on_front' => 1,
  28. 'is_visible_on_front' => 0,
  29. 'used_in_product_listing' => 0,
  30. 'used_for_sort_by' => 0,
  31. 'frontend_label' => ['Select Attribute'],
  32. 'backend_type' => 'varchar',
  33. 'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
  34. 'option' => [
  35. 'value' => [
  36. 'chair' => ['Chair'],
  37. 'table' => ['Table'],
  38. ],
  39. 'order' => [
  40. 'chair' => 1,
  41. 'table' => 2,
  42. ],
  43. ],
  44. ]
  45. );
  46. $selectAttribute->save();
  47. $installer->addAttributeToGroup(
  48. 'catalog_product',
  49. 'Default',
  50. 'General',
  51. $selectAttribute->getId()
  52. );
  53. /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $selectOptions */
  54. $selectOptions = $objectManager->create(
  55. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
  56. );
  57. $selectOptions->setAttributeFilter($selectAttribute->getId());
  58. $selectOptionsIds = $selectOptions->getAllIds();