Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
products_with_layered_navigation_attribute.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
10 
11 $eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
12 $attribute = $eavConfig->getAttribute('catalog_product', 'test_configurable');
13 
14 $eavConfig->clear();
15 
17 $installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
18 
19 if (!$attribute->getId()) {
20 
22  $attribute = Bootstrap::getObjectManager()->create(
23  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
24  );
25 
27  $attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepositoryInterface::class);
28 
29  $attribute->setData(
30  [
31  'attribute_code' => 'test_configurable',
32  'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
33  'is_global' => 1,
34  'is_user_defined' => 1,
35  'frontend_input' => 'select',
36  'is_unique' => 0,
37  'is_required' => 0,
38  'is_searchable' => 1,
39  'is_visible_in_advanced_search' => 0,
40  'is_comparable' => 1,
41  'is_filterable' => 1,
42  'is_filterable_in_search' => 1,
43  'is_used_for_promo_rules' => 0,
44  'is_html_allowed_on_front' => 1,
45  'is_visible_on_front' => 1,
46  'used_in_product_listing' => 1,
47  'used_for_sort_by' => 1,
48  'frontend_label' => ['Test Configurable'],
49  'backend_type' => 'int',
50  'option' => [
51  'value' => ['option_0' => ['Option 1'], 'option_1' => ['Option 2']],
52  'order' => ['option_0' => 1, 'option_1' => 2],
53  ],
54  'default' => ['option_0']
55  ]
56  );
57 
59 
60  /* Assign attribute to attribute set */
61  $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
62 }
63 
64 $eavConfig->clear();
65 
68 
71 $product->isObjectNew(true);
72 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
73  ->setId(10)
74  ->setAttributeSetId(4)
75  ->setName('Simple Product1')
76  ->setSku('simple1')
77  ->setTaxClassId('none')
78  ->setDescription('description')
79  ->setShortDescription('short description')
80  ->setOptionsContainer('container1')
81  ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
82  ->setPrice(10)
83  ->setWeight(1)
84  ->setMetaTitle('meta title')
85  ->setMetaKeyword('meta keyword')
86  ->setMetaDescription('meta description')
87  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
88  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
89  ->setWebsiteIds([1])
90  ->setCategoryIds([])
91  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
92  ->setSpecialPrice('5.99')
93  ->save();
94 
95 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
96 $product->isObjectNew(true);
97 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
98  ->setId(11)
99  ->setAttributeSetId(4)
100  ->setName('Simple Product2')
101  ->setSku('simple2')
102  ->setTaxClassId('none')
103  ->setDescription('description')
104  ->setShortDescription('short description')
105  ->setOptionsContainer('container1')
106  ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
107  ->setPrice(20)
108  ->setWeight(1)
109  ->setMetaTitle('meta title')
110  ->setMetaKeyword('meta keyword')
111  ->setMetaDescription('meta description')
112  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
113  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
114  ->setWebsiteIds([1])
115  ->setCategoryIds([])
116  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
117  ->setSpecialPrice('15.99')
118  ->save();
119 
120 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
121 $product->isObjectNew(true);
122 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
123  ->setId(12)
124  ->setAttributeSetId(4)
125  ->setName('Simple Product3')
126  ->setSku('simple3')
127  ->setTaxClassId('none')
128  ->setDescription('description')
129  ->setShortDescription('short description')
130  ->setPrice(30)
131  ->setWeight(1)
132  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
133  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)
134  ->setWebsiteIds([1])
135  ->setCategoryIds([])
136  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 140, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
137  ->setSpecialPrice('25.99')
138  ->save();
139 
141 $category->isObjectNew(true);
142 $category->setId(
143  333
144 )->setCreatedAt(
145  '2014-06-23 09:50:07'
146 )->setName(
147  'Category 1'
148 )->setParentId(
149  2
150 )->setPath(
151  '1/2/333'
152 )->setLevel(
153  2
154 )->setAvailableSortBy(
155  ['position', 'name']
156 )->setDefaultSortBy(
157  'name'
158 )->setIsActive(
159  true
160 )->setPosition(
161  1
162 )->setPostedProducts(
163  [10 => 10, 11 => 11, 12 => 12]
164 )->save();
165 
167 $indexerCollection = Bootstrap::getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class);
168 $indexerCollection->load();
170 foreach ($indexerCollection->getItems() as $indexer) {
171  $indexer->reindexAll();
172 }