Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
configurable_products.php
Go to the documentation of this file.
1 <?php
17 
18 require __DIR__ . '/select_attribute.php';
19 require __DIR__ . '/multiselect_attribute.php';
20 require __DIR__ . '/configurable_attribute.php';
21 
22 $objectManager = Bootstrap::getObjectManager();
23 
25 $installer = $objectManager->create(CategorySetup::class);
26 
28 $productRepository = $objectManager->get(ProductRepositoryInterface::class);
29 
30 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
31 
33 $options = $attribute->getOptions();
34 
36 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
38 $productIds = [10, 20];
39 array_shift($options);
40 foreach ($options as $option) {
42  $product = $objectManager->create(Product::class);
43  $productId = array_shift($productIds);
44  $product->setTypeId(Type::TYPE_SIMPLE)
45  ->setId($productId)
46  ->setAttributeSetId($attributeSetId)
47  ->setWebsiteIds([1])
48  ->setName('Configurable Option Product' . str_replace(' ', '', $option->getLabel()))
49  ->setSku('simple_' . $productId)
50  ->setPrice($productId)
51  ->setTestConfigurable($option->getValue())
52  ->setShortDescription('simpledescription')
53  ->setMultiselectAttribute([$multiselectOptionsIds[0]])
54  ->setSelectAttribute([$selectOptionsIds[0]])
55  ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE)
56  ->setStatus(Status::STATUS_ENABLED)
57  ->setStockData(
58  [
59  'use_config_manage_stock' => 1,
60  'qty' => 100,
61  'is_qty_decimal' => 0,
62  'is_in_stock' => 1
63  ]
64  );
66 
67  $attributeValues[] = [
68  'label' => 'test',
69  'attribute_id' => $attribute->getId(),
70  'value_index' => $option->getValue(),
71  ];
72  $associatedProductIds[] = $product->getId();
73 }
74 
76 $product = $objectManager->create(Product::class);
78 $optionsFactory = $objectManager->create(Factory::class);
80  [
81  'attribute_id' => $attribute->getId(),
82  'code' => $attribute->getAttributeCode(),
83  'label' => $attribute->getStoreLabel(),
84  'position' => '0',
85  'values' => $attributeValues,
86  ],
87 ];
89 $extensionConfigurableAttributes = $product->getExtensionAttributes();
90 $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
91 $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
92 $product->setExtensionAttributes($extensionConfigurableAttributes);
93 
94 $product->setTypeId(Configurable::TYPE_CODE)
95  ->setId(1)
96  ->setAttributeSetId($attributeSetId)
97  ->setWebsiteIds([1])
98  ->setName('Configurable Product')
99  ->setSku('configurable')
100  ->setVisibility(Visibility::VISIBILITY_BOTH)
101  ->setStatus(Status::STATUS_ENABLED)
102  ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]);
103 $productRepository->cleanCache();
$selectOptionsIds
$associatedProductIds
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$multiselectOptionsIds
$extensionConfigurableAttributes
$configurableAttributesData
$configurableOptions