Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_configurable.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');
45 $productIds = [10, 20];
46 array_shift($options); //remove the first option which is empty
47 
48 foreach ($options as $option) {
50  $product = Bootstrap::getObjectManager()->create(Product::class);
51  $productId = array_shift($productIds);
52  $product->setTypeId(Type::TYPE_SIMPLE)
53  ->setId($productId)
54  ->setAttributeSetId($attributeSetId)
55  ->setWebsiteIds($websiteIds)
56  ->setName('Configurable Option' . $option->getLabel())
57  ->setSku('simple_' . $productId)
58  ->setPrice($productId)
59  ->setTestConfigurable($option->getValue())
60  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
61  ->setStatus(Status::STATUS_ENABLED)
62  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
63 
65 
67  $stockItem = Bootstrap::getObjectManager()->create(\Magento\CatalogInventory\Model\Stock\Item::class);
68  $stockItem->load($productId, 'product_id');
69 
70  if (!$stockItem->getProductId()) {
71  $stockItem->setProductId($productId);
72  }
73  $stockItem->setUseConfigManageStock(1);
74  $stockItem->setQty(1000);
75  $stockItem->setIsQtyDecimal(0);
76  $stockItem->setIsInStock(1);
77  $stockItem->save();
78 
79  $attributeValues[] = [
80  'label' => 'test',
81  'attribute_id' => $attribute->getId(),
82  'value_index' => $option->getValue(),
83  ];
84  $associatedProductIds[] = $product->getId();
85 }
86 
88 $product = Bootstrap::getObjectManager()->create(Product::class);
89 
91 $optionsFactory = Bootstrap::getObjectManager()->create(Factory::class);
92 
94  [
95  'attribute_id' => $attribute->getId(),
96  'code' => $attribute->getAttributeCode(),
97  'label' => $attribute->getStoreLabel(),
98  'position' => '0',
99  'values' => $attributeValues,
100  ],
101 ];
102 
104 
105 $extensionConfigurableAttributes = $product->getExtensionAttributes();
106 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
107 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
108 
109 $product->setExtensionAttributes($extensionConfigurableAttributes);
110 
111 // Remove any previously created product with the same id.
113 $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
114 $registry->unregister('isSecureArea');
115 $registry->register('isSecureArea', true);
116 try {
117  $productToDelete = $productRepository->getById(1);
118  $productRepository->delete($productToDelete);
119 
121  $itemResource = Bootstrap::getObjectManager()->get(\Magento\Quote\Model\ResourceModel\Quote\Item::class);
122  $itemResource->getConnection()->delete(
123  $itemResource->getMainTable(),
124  'product_id = ' . $productToDelete->getId()
125  );
126 } catch (\Exception $e) {
127  // Nothing to remove
128 }
129 $registry->unregister('isSecureArea');
130 $registry->register('isSecureArea', false);
131 
132 $product->setTypeId(Configurable::TYPE_CODE)
133  ->setId(1)
134  ->setAttributeSetId($attributeSetId)
135  ->setWebsiteIds($websiteIds)
136  ->setName('Configurable Product')
137  ->setSku('configurable')
138  ->setVisibility(Visibility::VISIBILITY_BOTH)
139  ->setStatus(Status::STATUS_ENABLED)
140  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
141 
143 
146  ->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
147 
148 $categoryLinkManagement->assignProductToCategories(
149  $product->getSku(),
150  [2]
151 );
$productRepository
$associatedProductIds
$configurableAttributesData
$configurableOptions
$categoryLinkManagement
$extensionConfigurableAttributes