Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
filterable_attribute.php
Go to the documentation of this file.
1 <?php
7 /* Create attribute */
10  \Magento\Catalog\Setup\CategorySetup::class,
11  ['resourceName' => 'catalog_setup']
12 );
15  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
16 );
17 $selectAttribute->setData(
18  [
19  'attribute_code' => 'select_attribute',
20  'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
21  'is_global' => 1,
22  'frontend_input' => 'select',
23  'is_filterable' => 1,
24  'option' => [
25  'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']],
26  'order' => ['option_0' => 1, 'option_1' => 2],
27  ],
28  'backend_type' => 'int',
29  ]
30 );
31 $selectAttribute->save();
32 /* Assign attribute to attribute set */
33 $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $selectAttribute->getId());
34 
37  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
38 );
39 $selectOptions->setAttributeFilter($selectAttribute->getId());
40 
42  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
43 );
44 $multiselectAttribute->setData(
45  [
46  'attribute_code' => 'multiselect_attribute',
47  'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
48  'is_global' => 1,
49  'frontend_input' => 'multiselect',
50  'is_filterable' => 1,
51  'option' => [
52  'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']],
53  'order' => ['option_0' => 1, 'option_1' => 2],
54  ],
55  'backend_type' => 'varchar',
56  ]
57 );
58 $multiselectAttribute->save();
59 /* Assign attribute to attribute set */
60 $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $multiselectAttribute->getId());
61 
64  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
65 );
66 $multiselectOptions->setAttributeFilter($multiselectAttribute->getId());
67 
68 /* Create simple products per each select(dropdown) option */
69 foreach ($selectOptions as $option) {
72  \Magento\Catalog\Model\Product::class
73  );
74  $product->setTypeId(
75  \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE
76  )->setAttributeSetId(
77  $installer->getAttributeSetId('catalog_product', 'Default')
78  )->setWebsiteIds(
79  [1]
80  )->setName(
81  'Simple Product ' . $option->getId()
82  )->setSku(
83  'simple_product_' . $option->getId()
84  )->setPrice(
85  99
86  )->setCategoryIds(
87  [2]
88  )->setVisibility(
89  \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
90  )->setStatus(
91  \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
92  )->setStockData(
93  ['use_config_manage_stock' => 1, 'qty' => 5, 'is_in_stock' => 1]
94  )->save();
95 
97  \Magento\Catalog\Model\Product\Action::class
98  )->updateAttributes(
99  [$product->getId()],
100  [
101  $selectAttribute->getAttributeCode() => $option->getId(),
102  $multiselectAttribute->getAttributeCode() => $multiselectOptions->getLastItem()->getId()
103  ],
104  $product->getStoreId()
105  );
106 }
$multiselectOptions
$multiselectAttribute