Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
configurable_attribute.php
Go to the documentation of this file.
1 <?php
9 
10 $eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
11 $attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable');
12 
13 $eavConfig->clear();
14 
16 $installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
17 
18 if (!$attribute->getId()) {
19 
21  $attribute = Bootstrap::getObjectManager()->create(
22  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
23  );
24 
26  $attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
27 
28  $attribute->setData(
29  [
30  'attribute_code' => 'test_configurable',
31  'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
32  'is_global' => 1,
33  'is_user_defined' => 1,
34  'frontend_input' => 'select',
35  'is_unique' => 0,
36  'is_required' => 0,
37  'is_searchable' => 0,
38  'is_visible_in_advanced_search' => 0,
39  'is_comparable' => 0,
40  'is_filterable' => 0,
41  'is_filterable_in_search' => 0,
42  'is_used_for_promo_rules' => 0,
43  'is_html_allowed_on_front' => 1,
44  'is_visible_on_front' => 0,
45  'used_in_product_listing' => 0,
46  'used_for_sort_by' => 0,
47  'frontend_label' => ['Test Configurable'],
48  'backend_type' => 'int',
49  'option' => [
50  'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']],
51  'order' => ['option_0' => 1, 'option_1' => 2],
52  ],
53  ]
54  );
55 
57 
58  /* Assign attribute to attribute set */
59  $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
60 }
61 
62 $eavConfig->clear();