Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
default_stock_configurable_products.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
18 
20 // @codingStandardsIgnoreStart
21 require __DIR__ . '/../../../../../../dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/configurable_attribute.php';
22 // @codingStandardsIgnoreEnd
23 
25 $productRepository = Bootstrap::getObjectManager()
26  ->get(ProductRepositoryInterface::class);
27 
29 $installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
30 
31 /* Create simple products per each option value*/
33 $options = $attribute->getOptions();
34 
36 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
38 $productIds = [10, 20];
39 array_shift($options); //remove the first option which is empty
40 
41 foreach ($options as $option) {
43  $product = Bootstrap::getObjectManager()->create(Product::class);
44  $productId = array_shift($productIds);
45  $product->setTypeId(Type::TYPE_SIMPLE)
46  ->setId($productId)
47  ->setAttributeSetId($attributeSetId)
48  ->setWebsiteIds([1])
49  ->setName('Configurable Option' . $option->getLabel())
50  ->setSku('simple_' . $productId)
51  ->setPrice($productId)
52  ->setTestConfigurable($option->getValue())
53  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
54  ->setStatus(Status::STATUS_ENABLED)
55  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
57 
58  $attributeValues[] = [
59  'label' => 'test',
60  'attribute_id' => $attribute->getId(),
61  'value_index' => $option->getValue(),
62  ];
63  $associatedProductIds[] = $product->getId();
64 }
65 
67 $product = Bootstrap::getObjectManager()->create(Product::class);
69 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
71  [
72  'attribute_id' => $attribute->getId(),
73  'code' => $attribute->getAttributeCode(),
74  'label' => $attribute->getStoreLabel(),
75  'position' => '0',
76  'values' => $attributeValues,
77  ],
78 ];
80 $extensionConfigurableAttributes = $product->getExtensionAttributes();
81 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
82 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
83 $product->setExtensionAttributes($extensionConfigurableAttributes);
84 
85 $product->setTypeId(Configurable::TYPE_CODE)
86  ->setId(1)
87  ->setAttributeSetId($attributeSetId)
88  ->setWebsiteIds([1])
89  ->setName('Configurable Product In Stock')
90  ->setSku('configurable_in_stock')
91  ->setVisibility(Visibility::VISIBILITY_BOTH)
92  ->setStatus(Status::STATUS_ENABLED)
93  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
94 $productRepository->cleanCache();
96 
97 /* Create simple products per each option value*/
99 $options = $attribute->getOptions();
100 
101 $attributeValues = [];
102 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
104 $productIds = [30, 40];
105 array_shift($options); //remove the first option which is empty
106 
107 foreach ($options as $option) {
109  $product = Bootstrap::getObjectManager()->create(Product::class);
110  $productId = array_shift($productIds);
111  $product->setTypeId(Type::TYPE_SIMPLE)
112  ->setId($productId)
113  ->setAttributeSetId($attributeSetId)
114  ->setWebsiteIds([1])
115  ->setName('Configurable Option' . $option->getLabel())
116  ->setSku('simple_' . $productId)
117  ->setPrice($productId)
118  ->setTestConfigurable($option->getValue())
119  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
120  ->setStatus(Status::STATUS_ENABLED)
121  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 0, 'is_qty_decimal' => 0, 'is_in_stock' => 0]);
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(11)
158  ->setAttributeSetId($attributeSetId)
159  ->setWebsiteIds([1])
160  ->setName('Configurable Product Out Of Stock')
161  ->setSku('configurable_out_of_stock')
162  ->setVisibility(Visibility::VISIBILITY_BOTH)
163  ->setStatus(Status::STATUS_ENABLED)
164  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
165 $productRepository->cleanCache();
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60