|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 7 | +use Magento\Catalog\Model\Product; |
| 8 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 9 | +use Magento\Catalog\Model\Product\Type; |
| 10 | +use Magento\Catalog\Model\Product\Visibility; |
| 11 | +use Magento\Catalog\Setup\CategorySetup; |
| 12 | +use Magento\ConfigurableProduct\Helper\Product\Options\Factory; |
| 13 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 14 | +use Magento\Eav\Api\Data\AttributeOptionInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | + |
| 17 | +require __DIR__ . '/configurable_attribute.php'; |
| 18 | +require __DIR__ . '/configurable_attribute_2.php'; |
| 19 | + |
| 20 | +/** @var ProductRepositoryInterface $productRepository */ |
| 21 | +$productRepository = Bootstrap::getObjectManager() |
| 22 | + ->get(ProductRepositoryInterface::class); |
| 23 | + |
| 24 | +/** @var $installer CategorySetup */ |
| 25 | +$installer = Bootstrap::getObjectManager()->create(CategorySetup::class); |
| 26 | + |
| 27 | +/* Create simple products per each option value*/ |
| 28 | +/** @var AttributeOptionInterface[] $options */ |
| 29 | +$options = $attribute->getOptions(); |
| 30 | + |
| 31 | +$attributeValues = []; |
| 32 | +$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default'); |
| 33 | +$associatedProductIds = []; |
| 34 | +$productIds = [10, 20]; |
| 35 | +array_shift($options); //remove the first option which is empty |
| 36 | + |
| 37 | +foreach ($options as $option) { |
| 38 | + /** @var $product Product */ |
| 39 | + $product = Bootstrap::getObjectManager()->create(Product::class); |
| 40 | + $productId = array_shift($productIds); |
| 41 | + $product->setTypeId(Type::TYPE_SIMPLE) |
| 42 | + ->setId($productId) |
| 43 | + ->setAttributeSetId($attributeSetId) |
| 44 | + ->setWebsiteIds([1]) |
| 45 | + ->setName('Configurable Option' . $option->getLabel()) |
| 46 | + ->setSku('simple_' . $productId) |
| 47 | + ->setPrice($productId) |
| 48 | + ->setTestConfigurable($option->getValue()) |
| 49 | + ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE) |
| 50 | + ->setStatus(Status::STATUS_ENABLED) |
| 51 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]); |
| 52 | + $product = $productRepository->save($product); |
| 53 | + |
| 54 | + $attributeValues[] = [ |
| 55 | + 'label' => 'test', |
| 56 | + 'attribute_id' => $attribute->getId(), |
| 57 | + 'value_index' => $option->getValue(), |
| 58 | + ]; |
| 59 | + $associatedProductIds[] = $product->getId(); |
| 60 | +} |
| 61 | + |
| 62 | +/** @var $product Product */ |
| 63 | +$product = Bootstrap::getObjectManager()->create(Product::class); |
| 64 | +/** @var Factory $optionsFactory */ |
| 65 | +$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class); |
| 66 | +$configurableAttributesData = [ |
| 67 | + [ |
| 68 | + 'attribute_id' => $attribute->getId(), |
| 69 | + 'code' => $attribute->getAttributeCode(), |
| 70 | + 'label' => $attribute->getStoreLabel(), |
| 71 | + 'position' => '0', |
| 72 | + 'values' => $attributeValues, |
| 73 | + ], |
| 74 | +]; |
| 75 | +$configurableOptions = $optionsFactory->create($configurableAttributesData); |
| 76 | +$extensionConfigurableAttributes = $product->getExtensionAttributes(); |
| 77 | +$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions); |
| 78 | +$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds); |
| 79 | +$product->setExtensionAttributes($extensionConfigurableAttributes); |
| 80 | + |
| 81 | +$product->setTypeId(Configurable::TYPE_CODE) |
| 82 | + ->setId(1) |
| 83 | + ->setAttributeSetId($attributeSetId) |
| 84 | + ->setWebsiteIds([1]) |
| 85 | + ->setName('Configurable Product') |
| 86 | + ->setSku('configurable') |
| 87 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 88 | + ->setStatus(Status::STATUS_ENABLED) |
| 89 | + ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]); |
| 90 | +$productRepository->cleanCache(); |
| 91 | +$productRepository->save($product); |
| 92 | + |
| 93 | +/* Create simple products per each option value*/ |
| 94 | +/** @var AttributeOptionInterface[] $options */ |
| 95 | +$options = $attribute2->getOptions(); |
| 96 | + |
| 97 | +$attributeValues = []; |
| 98 | +$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default'); |
| 99 | +$associatedProductIds = []; |
| 100 | +$productIds = [30, 40]; |
| 101 | +array_shift($options); //remove the first option which is empty |
| 102 | + |
| 103 | +foreach ($options as $option) { |
| 104 | + /** @var $product Product */ |
| 105 | + $product = Bootstrap::getObjectManager()->create(Product::class); |
| 106 | + $productId = array_shift($productIds); |
| 107 | + $product->setTypeId(Type::TYPE_SIMPLE) |
| 108 | + ->setId($productId) |
| 109 | + ->setAttributeSetId($attributeSetId) |
| 110 | + ->setWebsiteIds([1]) |
| 111 | + ->setName('Configurable Option' . $option->getLabel()) |
| 112 | + ->setSku('simple_' . $productId) |
| 113 | + ->setPrice($productId) |
| 114 | + ->setTestConfigurable2($option->getValue()) |
| 115 | + ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE) |
| 116 | + ->setStatus(Status::STATUS_ENABLED) |
| 117 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]); |
| 118 | + $product = $productRepository->save($product); |
| 119 | + |
| 120 | + $attributeValues[] = [ |
| 121 | + 'label' => 'test', |
| 122 | + 'attribute_id' => $attribute2->getId(), |
| 123 | + 'value_index' => $option->getValue(), |
| 124 | + ]; |
| 125 | + $associatedProductIds[] = $product->getId(); |
| 126 | +} |
| 127 | + |
| 128 | +/** @var $product Product */ |
| 129 | +$product = Bootstrap::getObjectManager()->create(Product::class); |
| 130 | + |
| 131 | +/** @var Factory $optionsFactory */ |
| 132 | +$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class); |
| 133 | + |
| 134 | +$configurableAttributesData = [ |
| 135 | + [ |
| 136 | + 'attribute_id' => $attribute2->getId(), |
| 137 | + 'code' => $attribute2->getAttributeCode(), |
| 138 | + 'label' => $attribute2->getStoreLabel(), |
| 139 | + 'position' => '1', |
| 140 | + 'values' => $attributeValues, |
| 141 | + ], |
| 142 | +]; |
| 143 | + |
| 144 | +$configurableOptions = $optionsFactory->create($configurableAttributesData); |
| 145 | + |
| 146 | +$extensionConfigurableAttributes = $product->getExtensionAttributes(); |
| 147 | +$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions); |
| 148 | +$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds); |
| 149 | + |
| 150 | +$product->setExtensionAttributes($extensionConfigurableAttributes); |
| 151 | + |
| 152 | +$product->setTypeId(Configurable::TYPE_CODE) |
| 153 | + ->setId(11) |
| 154 | + ->setAttributeSetId($attributeSetId) |
| 155 | + ->setWebsiteIds([1]) |
| 156 | + ->setName('Configurable Product 12345') |
| 157 | + ->setSku('configurable_12345') |
| 158 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 159 | + ->setStatus(Status::STATUS_ENABLED) |
| 160 | + ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]); |
| 161 | +$productRepository->cleanCache(); |
| 162 | +$productRepository->save($product); |
0 commit comments