Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
dropdown_attribute.php
Go to the documentation of this file.
1 <?php
7 /* Create attribute */
10  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
11 );
12 
13 if (!$attribute->loadByCode(4, 'dropdown_attribute')->getId()) {
16  \Magento\Catalog\Setup\CategorySetup::class
17  );
18 
19  $attribute->setData(
20  [
21  'attribute_code' => 'dropdown_attribute',
22  'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
23  'is_global' => 0,
24  'is_user_defined' => 1,
25  'frontend_input' => 'select',
26  'is_unique' => 0,
27  'is_required' => 0,
28  'is_searchable' => 0,
29  'is_visible_in_advanced_search' => 0,
30  'is_comparable' => 0,
31  'is_filterable' => 0,
32  'is_filterable_in_search' => 0,
33  'is_used_for_promo_rules' => 0,
34  'is_html_allowed_on_front' => 1,
35  'is_visible_on_front' => 1,
36  'used_in_product_listing' => 1,
37  'used_for_sort_by' => 0,
38  'frontend_label' => ['Drop-Down Attribute'],
39  'backend_type' => 'varchar',
40  'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
41  'option' => [
42  'value' => [
43  'option_1' => ['Option 1'],
44  'option_2' => ['Option 2'],
45  'option_3' => ['Option 3'],
46  ],
47  'order' => [
48  'option_1' => 1,
49  'option_2' => 2,
50  'option_3' => 3,
51  ],
52  ],
53  ]
54  );
55  $attribute->save();
56 
57  /* Assign attribute to attribute set */
58  $installer->addAttributeToGroup('catalog_product', 'Default', 'Attributes', $attribute->getId());
59 }