Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_configurable_multiple.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
20 
22 
24 $productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
25 
27 $installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
28 
30 $website = Bootstrap::getObjectManager()->create(Website::class);
31 $website->load('us_website', 'code');
32 $websiteIds = [$website->getId()];
33 
35 $eavConfig = Bootstrap::getObjectManager()->create(Config::class);
36 $attribute = $eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_configurable');
37 
38 /* Create simple products per each option value*/
40 $options = $attribute->getOptions();
41 
43 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
44 
45 foreach ($configurableIds as $configurableId) {
46  $attributeValues = [];
48  $productIds = [10, 20, 30];
49  array_shift($options); //remove the first option which is empty
50 
51  foreach ($options as $option) {
53  $product = Bootstrap::getObjectManager()->create(Product::class);
54  $productId = $configurableId + array_shift($productIds);
55  $product->setTypeId(Type::TYPE_SIMPLE)
56  ->setId($productId)
57  ->setAttributeSetId($attributeSetId)
58  ->setWebsiteIds($websiteIds)
59  ->setName('Configurable Option' . $option->getLabel())
60  ->setSku('simple_' . $productId)
61  ->setPrice($productId)
62  ->setTestConfigurable($option->getValue())
63  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
64  ->setStatus(Status::STATUS_ENABLED)
65  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
66 
68 
70  $stockItem = Bootstrap::getObjectManager()->create(\Magento\CatalogInventory\Model\Stock\Item::class);
71  $stockItem->load($productId, 'product_id');
72 
73  if (!$stockItem->getProductId()) {
74  $stockItem->setProductId($productId);
75  }
76  $stockItem->setUseConfigManageStock(1);
77  $stockItem->setQty(1000);
78  $stockItem->setIsQtyDecimal(0);
79  $stockItem->setIsInStock(1);
80  $stockItem->save();
81 
82  $attributeValues[] = [
83  'label' => 'test',
84  'attribute_id' => $attribute->getId(),
85  'value_index' => $option->getValue(),
86  ];
87  $associatedProductIds[] = $product->getId();
88  }
89 
91  $product = Bootstrap::getObjectManager()->create(Product::class);
92 
94  $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
95 
97  [
98  'attribute_id' => $attribute->getId(),
99  'code' => $attribute->getAttributeCode(),
100  'label' => $attribute->getStoreLabel(),
101  'position' => '0',
102  'values' => $attributeValues,
103  ],
104  ];
105 
107 
108  $extensionConfigurableAttributes = $product->getExtensionAttributes();
109  $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
110  $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
111 
112  $product->setExtensionAttributes($extensionConfigurableAttributes);
113 
114  // Remove any previously created product with the same id.
116  $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
117  $registry->unregister('isSecureArea');
118  $registry->register('isSecureArea', true);
119  try {
120  $productToDelete = $productRepository->getById($configurableId);
121  $productRepository->delete($productToDelete);
122 
124  $itemResource = Bootstrap::getObjectManager()->get(\Magento\Quote\Model\ResourceModel\Quote\Item::class);
125  $itemResource->getConnection()->delete(
126  $itemResource->getMainTable(),
127  'product_id = ' . $productToDelete->getId()
128  );
129  } catch (\Exception $e) {
130  // Nothing to remove
131  }
132  $registry->unregister('isSecureArea');
133  $registry->register('isSecureArea', false);
134 
135  $product->setTypeId(Configurable::TYPE_CODE)
136  ->setId($configurableId)
137  ->setAttributeSetId($attributeSetId)
138  ->setWebsiteIds($websiteIds)
139  ->setName('Configurable Product ' . $configurableId)
140  ->setSku('configurable_' . $configurableId)
141  ->setVisibility(Visibility::VISIBILITY_BOTH)
142  ->setStatus(Status::STATUS_ENABLED)
143  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
144 
146 
149  ->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
150 
151  $categoryLinkManagement->assignProductToCategories(
152  $product->getSku(),
153  [2]
154  );
155 }
$categoryLinkManagement
$configurableAttributesData
$extensionConfigurableAttributes