Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_configurable_with_single_child.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
19 
20 Bootstrap::getInstance()->reinitialize();
21 
22 require __DIR__ . '/../../../Magento/ConfigurableProduct/_files/configurable_attribute.php';
23 
25 $productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
26 
28 $installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
29 
30 /* Create simple products per each option value*/
32 $options = $attribute->getOptions();
33 
35 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
36 array_shift($options); //remove the first option which is empty
37 
38 $option = reset($options);
39 
41 $childProduct = Bootstrap::getObjectManager()->create(Product::class);
42 $childProduct->setTypeId(Type::TYPE_SIMPLE)
43  ->setAttributeSetId($attributeSetId)
44  ->setName('Configurable Product Option' . $option->getLabel())
45  ->setSku('configurable_option_single_child')
46  ->setPrice(11)
47  ->setTestConfigurable($option->getValue())
48  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
49  ->setStatus(Status::STATUS_ENABLED)
50  ->setStockData(
51  [
52  'use_config_manage_stock' => 1,
53  'qty' => 100,
54  'is_qty_decimal' => 0,
55  'is_in_stock' => 1,
56  ]
57  );
59 
61  'label' => 'test',
62  'attribute_id' => $attribute->getId(),
63  'value_index' => $option->getValue(),
64 ];
65 
67 $configurableProduct = Bootstrap::getObjectManager()->create(Product::class);
68 
70 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
71 
73  [
74  'attribute_id' => $attribute->getId(),
75  'code' => $attribute->getAttributeCode(),
76  'label' => $attribute->getStoreLabel(),
77  'position' => '0',
78  'values' => $attributeValues,
79  ],
80 ];
81 
83 
85 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
86 $extensionConfigurableAttributes->setConfigurableProductLinks([$childProduct->getId()]);
87 
89 
90 $configurableProduct->setTypeId(Configurable::TYPE_CODE)
91  ->setAttributeSetId($attributeSetId)
92  ->setName('Configurable Product with single child')
93  ->setSku('configurable_with_single_child')
94  ->setVisibility(Visibility::VISIBILITY_BOTH)
95  ->setStatus(Status::STATUS_ENABLED)
96  ->setStockData(
97  [
98  'use_config_manage_stock' => 1,
99  'is_in_stock' => 1,
100  ]
101  );
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
reinitialize(array $overriddenParams=[])
Definition: Bootstrap.php:106