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