Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_configurable_sku.php
Go to the documentation of this file.
1 <?php
17 
19 
20 require __DIR__ . '/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 $productIds = [10, 20];
37 array_shift($options); //remove the first option which is empty
38 
39 foreach ($options as $option) {
41  $product = Bootstrap::getObjectManager()->create(Product::class);
42  $productId = array_shift($productIds);
43  $product->setTypeId(Type::TYPE_SIMPLE)
44  ->setId($productId)
45  ->setAttributeSetId($attributeSetId)
46  ->setWebsiteIds([1])
47  ->setName('Configurable Option' . $option->getLabel())
48  ->setSku('simple_' . $productId)
49  ->setPrice($productId)
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]);
54 
56 
58  $stockItem = Bootstrap::getObjectManager()->create(\Magento\CatalogInventory\Model\Stock\Item::class);
59  $stockItem->load($productId, 'product_id');
60 
61  if (!$stockItem->getProductId()) {
62  $stockItem->setProductId($productId);
63  }
64  $stockItem->setUseConfigManageStock(1);
65  $stockItem->setQty(1000);
66  $stockItem->setIsQtyDecimal(0);
67  $stockItem->setIsInStock(1);
68  $stockItem->save();
69 
70  $attributeValues[] = [
71  'label' => 'test',
72  'attribute_id' => $attribute->getId(),
73  'value_index' => $option->getValue(),
74  ];
75  $associatedProductIds[] = $product->getId();
76 }
77 
79 $product = Bootstrap::getObjectManager()->create(Product::class);
80 
82 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
83 
85  [
86  'attribute_id' => $attribute->getId(),
87  'code' => $attribute->getAttributeCode(),
88  'label' => $attribute->getStoreLabel(),
89  'position' => '0',
90  'values' => $attributeValues,
91  ],
92 ];
93 
95 
96 $extensionConfigurableAttributes = $product->getExtensionAttributes();
97 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
98 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
99 
100 $product->setExtensionAttributes($extensionConfigurableAttributes);
101 
102 // Remove any previously created product with the same id.
104 $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
105 $registry->unregister('isSecureArea');
106 $registry->register('isSecureArea', true);
107 try {
108  $productToDelete = $productRepository->getById(1);
109  $productRepository->delete($productToDelete);
110 
112  $itemResource = Bootstrap::getObjectManager()->get(\Magento\Quote\Model\ResourceModel\Quote\Item::class);
113  $itemResource->getConnection()->delete(
114  $itemResource->getMainTable(),
115  'product_id = ' . $productToDelete->getId()
116  );
117 } catch (\Exception $e) {
118  // Nothing to remove
119 }
120 $registry->unregister('isSecureArea');
121 $registry->register('isSecureArea', false);
122 
123 $product->setTypeId(Configurable::TYPE_CODE)
124  ->setAttributeSetId($attributeSetId)
125  ->setWebsiteIds([1])
126  ->setName('Configurable Product SKU')
127  ->setSku('configurable')
128  ->setVisibility(Visibility::VISIBILITY_BOTH)
129  ->setStatus(Status::STATUS_ENABLED)
130  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
131 
133 
136  ->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
137 
138 $categoryLinkManagement->assignProductToCategories(
139  $product->getSku(),
140  [2]
141 );
$configurableAttributesData
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$extensionConfigurableAttributes