Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_configurable_custom.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 require __DIR__ . '/configurable_attribute_with_source_model.php';
9 
11 
12 $installer = $objectManager->create(\Magento\Catalog\Setup\CategorySetup::class);
13 
14 $eavConfig = $objectManager->get(\Magento\Eav\Model\Config::class);
15 $attribute = $eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_configurable_with_sm');
16 $options = $attribute->getOptions();
17 
18 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
21 $attributeSetId = $installer->getAttributeSetId(\Magento\Catalog\Model\Product::ENTITY, 'Default');
22 
23 foreach ($options as $key => $option) {
24  $productId = $key + 10;
25 
26  $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
27  $product->setTypeId(Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
28  ->setId($productId)
29  ->setAttributeSetId($attributeSetId)
30  ->setWebsiteIds([1])
31  ->setName('Configurable Option ' . $option->getLabel())
32  ->setSku('simple_' . $productId)
33  ->setPrice($productId)
34  ->setTestConfigurableWithSm($option->getValue())
35  ->setVisibility(Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE)
36  ->setStatus(Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
37  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
39 
40  $stockItem = $objectManager->create(\Magento\CatalogInventory\Model\Stock\Item::class);
41  $stockItem->load($productId, 'product_id');
42  if (!$stockItem->getProductId()) {
43  $stockItem->setProductId($productId);
44  }
45  $stockItem->setUseConfigManageStock(1);
46  $stockItem->setQty(1000);
47  $stockItem->setIsQtyDecimal(0);
48  $stockItem->setIsInStock(1);
49  $stockItem->save();
50 
51  $attributeValues[] = [
52  'label' => 'test',
53  'attribute_id' => $attribute->getId(),
54  'value_index' => $option->getValue(),
55  ];
56  $associatedProductIds[] = $product->getId();
57 }
58 
59 $optionsFactory = $objectManager->create(\Magento\ConfigurableProduct\Helper\Product\Options\Factory::class);
61  [
62  'attribute_id' => $attribute->getId(),
63  'code' => $attribute->getAttributeCode(),
64  'label' => $attribute->getStoreLabel(),
65  'position' => '0',
66  'values' => $attributeValues,
67  ],
68 ];
70 
71 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
72 $extensionConfigurableAttributes = $product->getExtensionAttributes();
73 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
74 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
75 $product->setExtensionAttributes($extensionConfigurableAttributes);
76 
77 $product->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)
78  ->setId(1)
79  ->setAttributeSetId($attributeSetId)
80  ->setWebsiteIds([1])
81  ->setName('Configurable Product')
82  ->setSku('configurable')
83  ->setVisibility(Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
84  ->setStatus(Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
85  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
87 
88 $categoryLinkManagement = $objectManager->create(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);
89 $categoryLinkManagement->assignProductToCategories(
90  $product->getSku(),
91  [2]
92 );
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$extensionConfigurableAttributes
foreach($options as $key=> $option) $optionsFactory