Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
multiple_mixed_products.php
Go to the documentation of this file.
1 <?php
17 
18 require __DIR__ . '/configurable_attribute.php';
19 
21 $productRepository = Bootstrap::getObjectManager()
22  ->get(ProductRepositoryInterface::class);
23 
25 $installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
26 
27 /* Create simple products per each option value*/
29 $options = $attribute->getOptions();
30 
32 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
34 $productIds = [31, 32];
35 array_shift($options); //remove the first option which is empty
36 
37 foreach ($options as $option) {
39  $product = Bootstrap::getObjectManager()->create(Product::class);
40  $productId = array_shift($productIds);
41  $product->setTypeId(Type::TYPE_SIMPLE)
42  ->setId($productId)
43  ->setAttributeSetId($attributeSetId)
44  ->setWebsiteIds([1])
45  // ->setName('Configurable Option' . $option->getLabel())
46  ->setName('ConfigurableOption' .$productId .'_' . $option->getLabel())
47  ->setSku('simple_' . $productId)
48  ->setPrice($productId)
49  ->setWeight(1)
50  ->setTestConfigurable($option->getValue())
51  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
52  ->setStatus(Status::STATUS_ENABLED)
53  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
55 
56  $attributeValues[] = [
57  'label' => 'test',
58  'attribute_id' => $attribute->getId(),
59  'value_index' => $option->getValue(),
60  ];
61  $associatedProductIds[] = $product->getId();
62 }
63 
65 $product = Bootstrap::getObjectManager()->create(Product::class);
67 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
69  [
70  'attribute_id' => $attribute->getId(),
71  'code' => $attribute->getAttributeCode(),
72  'label' => $attribute->getStoreLabel(),
73  'position' => '0',
74  'values' => $attributeValues,
75  ],
76 ];
78 $extensionConfigurableAttributes = $product->getExtensionAttributes();
79 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
80 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
81 $product->setExtensionAttributes($extensionConfigurableAttributes);
82 
83 $product->setTypeId(Configurable::TYPE_CODE)
84  ->setId(1)
85  ->setAttributeSetId($attributeSetId)
86  ->setWebsiteIds([1])
87  ->setName('Configurable Product')
88  ->setSku('configurable')
89  ->setVisibility(Visibility::VISIBILITY_BOTH)
90  ->setStatus(Status::STATUS_ENABLED)
91  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
92 $productRepository->cleanCache();
94 
95 /* Create simple products per each option value*/
97 $options = $attribute->getOptions();
98 
99 $attributeValues = [];
100 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
102 $productIds = [41, 42];
103 array_shift($options); //remove the first option which is empty
104 
105 foreach ($options as $option) {
107  $product = Bootstrap::getObjectManager()->create(Product::class);
108  $productId = array_shift($productIds);
109  $product->setTypeId(Type::TYPE_SIMPLE)
110  ->setId($productId)
111  ->setAttributeSetId($attributeSetId)
112  ->setWebsiteIds([1])
113  // ->setName('Configurable Option' . $option->getLabel())
114  ->setName('ConfigurableOption' .$productId .'_' . $option->getLabel())
115  ->setSku('simple_' . $productId)
116  ->setPrice($productId)
117  ->setWeight(2)
118  ->setTestConfigurable($option->getValue())
119  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
120  ->setStatus(Status::STATUS_ENABLED)
121  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
123 
124  $attributeValues[] = [
125  'label' => 'test',
126  'attribute_id' => $attribute->getId(),
127  'value_index' => $option->getValue(),
128  ];
129  $associatedProductIds[] = $product->getId();
130 }
131 
133 $product = Bootstrap::getObjectManager()->create(Product::class);
134 
136 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
137 
139  [
140  'attribute_id' => $attribute->getId(),
141  'code' => $attribute->getAttributeCode(),
142  'label' => $attribute->getStoreLabel(),
143  'position' => '0',
144  'values' => $attributeValues,
145  ],
146 ];
147 
149 
150 $extensionConfigurableAttributes = $product->getExtensionAttributes();
151 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
152 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
153 
154 $product->setExtensionAttributes($extensionConfigurableAttributes);
155 
156 $product->setTypeId(Configurable::TYPE_CODE)
157  ->setId(2)
158  ->setAttributeSetId($attributeSetId)
159  ->setWebsiteIds([1])
160  ->setName('Configurable Product 12345')
161  ->setSku('configurable_12345')
162  ->setVisibility(Visibility::VISIBILITY_BOTH)
163  ->setStatus(Status::STATUS_ENABLED)
164  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
165 $productRepository->cleanCache();
167 
169 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
170 $product->isObjectNew(true);
171 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
172  ->setId(10)
173  ->setAttributeSetId(4)
174  ->setName('Simple Product1')
175  ->setSku('simple1')
176  ->setTaxClassId('none')
177  ->setDescription('description')
178  ->setShortDescription('short description')
179  ->setOptionsContainer('container1')
180  ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
181  ->setPrice(10)
182  ->setWeight(1)
183  ->setMetaTitle('meta title')
184  ->setMetaKeyword('meta keyword')
185  ->setMetaDescription('meta description')
186  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
187  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
188  ->setWebsiteIds([1])
189  ->setCategoryIds([])
190  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
191  ->setSpecialPrice('5.99')
192  ->save();
193 
194 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
195 $product->isObjectNew(true);
196 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
197  ->setId(11)
198  ->setAttributeSetId(4)
199  ->setName('Simple Product2')
200  ->setSku('simple2')
201  ->setTaxClassId('none')
202  ->setDescription('description')
203  ->setShortDescription('short description')
204  ->setOptionsContainer('container1')
205  ->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
206  ->setPrice(20)
207  ->setWeight(1)
208  ->setMetaTitle('meta title')
209  ->setMetaKeyword('meta keyword')
210  ->setMetaDescription('meta description')
211  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_CATALOG)
212  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
213  ->setWebsiteIds([1])
214  ->setCategoryIds([])
215  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
216  ->setSpecialPrice('15.99')
217  ->save();
218 
219 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
220 $product->isObjectNew(true);
221 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
222  ->setId(12)
223  ->setAttributeSetId(4)
224  ->setName('Simple Product3')
225  ->setSku('simple3')
226  ->setTaxClassId('none')
227  ->setDescription('description')
228  ->setShortDescription('short description')
229  ->setPrice(30)
230  ->setWeight(1)
231  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_CATALOG)
232  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)
233  ->setWebsiteIds([1])
234  ->setCategoryIds([])
235  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 140, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
236  ->setSpecialPrice('25.99')
237  ->save();
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$configurableAttributesData
$extensionConfigurableAttributes