multiselect_attribute.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
  10. /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
  11. $multiselectAttribute = $objectManager->create(
  12. \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
  13. );
  14. $multiselectAttribute->setData(
  15. [
  16. 'attribute_code' => 'multiselect_attribute',
  17. 'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
  18. 'is_global' => 1,
  19. 'is_user_defined' => 1,
  20. 'frontend_input' => 'multiselect',
  21. 'is_unique' => 0,
  22. 'is_required' => 0,
  23. 'is_searchable' => 1,
  24. 'is_visible_in_advanced_search' => 0,
  25. 'is_comparable' => 0,
  26. 'is_filterable' => 1,
  27. 'is_filterable_in_search' => 0,
  28. 'is_used_for_promo_rules' => 0,
  29. 'is_html_allowed_on_front' => 1,
  30. 'is_visible_on_front' => 0,
  31. 'used_in_product_listing' => 0,
  32. 'used_for_sort_by' => 0,
  33. 'frontend_label' => ['Multiselect Attribute'],
  34. 'backend_type' => 'varchar',
  35. 'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
  36. 'option' => [
  37. 'value' => [
  38. 'dog' => ['Dog'],
  39. 'cat' => ['Cat'],
  40. ],
  41. 'order' => [
  42. 'dog' => 1,
  43. 'cat' => 2,
  44. ],
  45. ],
  46. ]
  47. );
  48. $multiselectAttribute->save();
  49. $installer->addAttributeToGroup(
  50. 'catalog_product',
  51. 'Default',
  52. 'General',
  53. $multiselectAttribute->getId()
  54. );
  55. /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $multiselectOptions */
  56. $multiselectOptions = $objectManager->create(
  57. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
  58. );
  59. $multiselectOptions->setAttributeFilter($multiselectAttribute->getId());
  60. $multiselectOptionsIds = $multiselectOptions->getAllIds();