Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_configurable_not_available.php
Go to the documentation of this file.
1 <?php
17 
18 Bootstrap::getInstance()->reinitialize();
19 
20 require __DIR__ . '/../../../Magento/Framework/Search/_files/configurable_attribute.php';
21 
23 $productRepository = Bootstrap::getObjectManager()
24  ->create(ProductRepositoryInterface::class);
25 
27 $installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
28 
29 /* Create simple products per each option value*/
31 $options = $attribute->getOptions();
32 
34 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
36 $productsSku = [1110, 1120];
37 array_shift($options); //remove the first option which is empty
38 
40 foreach ($options as $option) {
42  $product = Bootstrap::getObjectManager()->create(Product::class);
43  $productSku = array_shift($productsSku);
44  $product->setTypeId(Type::TYPE_SIMPLE)
45  ->setAttributeSetId($attributeSetId)
46  ->setName('Configurable Option' . $option->getLabel())
47  ->setSku('simple_not_avalilable_' . $productSku)
48  ->setPrice(11)
49  ->setTestConfigurable($option->getValue())
50  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
51  ->setStatus(Status::STATUS_DISABLED);
53 
55  $stockItem = $product->getExtensionAttributes()->getStockItem();
56  $stockItem->setUseConfigManageStock(1)->setIsInStock(true)->setQty(100)->setIsQtyDecimal(0);
57 
59 
60  $attributeValues[] = [
61  'label' => 'test',
62  'attribute_id' => $attribute->getId(),
63  'value_index' => $option->getValue(),
64  ];
65  $associatedProductIds[] = $product->getId();
66  $isFirstOption = false;
67 }
68 
70 $product = Bootstrap::getObjectManager()->create(Product::class);
71 
73 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
74 
76  [
77  'attribute_id' => $attribute->getId(),
78  'code' => $attribute->getAttributeCode(),
79  'label' => $attribute->getStoreLabel(),
80  'position' => '0',
81  'values' => $attributeValues,
82  ],
83 ];
84 
86 
87 $extensionConfigurableAttributes = $product->getExtensionAttributes();
88 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
89 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
90 
91 $product->setExtensionAttributes($extensionConfigurableAttributes);
92 
93 $product->setTypeId(Configurable::TYPE_CODE)
94  ->setAttributeSetId($attributeSetId)
95  ->setName('Configurable Product Disable Child Products')
96  ->setSku('configurable_not_available')
97  ->setVisibility(Visibility::VISIBILITY_BOTH)
98  ->setStatus(Status::STATUS_ENABLED);
100 
102 $stockItem = $product->getExtensionAttributes()->getStockItem();
103 $stockItem->setUseConfigManageStock(1)->setIsInStock(1);
104 
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
reinitialize(array $overriddenParams=[])
Definition: Bootstrap.php:106