|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +use Magento\Catalog\Api\Data\ProductExtensionFactory; |
| 9 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 12 | +use Magento\Catalog\Model\Product\Type as ProductType; |
| 13 | +use Magento\Catalog\Model\Product\Visibility; |
| 14 | +use Magento\Catalog\Model\ProductFactory; |
| 15 | +use Magento\ConfigurableProduct\Helper\Product\Options\Factory; |
| 16 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 17 | +use Magento\Store\Api\WebsiteRepositoryInterface; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\TestFramework\Workaround\Override\Fixture\Resolver; |
| 20 | + |
| 21 | +Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_website_with_two_stores.php'); |
| 22 | +Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/configurable_attribute.php'); |
| 23 | + |
| 24 | +$objectManager = Bootstrap::getObjectManager(); |
| 25 | +/** @var ProductAttributeRepositoryInterface $productAttributeRepository */ |
| 26 | +$productAttributeRepository = $objectManager->get(ProductAttributeRepositoryInterface::class); |
| 27 | +$attribute = $productAttributeRepository->get('test_configurable'); |
| 28 | +$options = $attribute->getOptions(); |
| 29 | +/** @var WebsiteRepositoryInterface $websiteRepository */ |
| 30 | +$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class); |
| 31 | +$baseWebsite = $websiteRepository->get('base'); |
| 32 | +$secondWebsite = $websiteRepository->get('test'); |
| 33 | +/** @var ProductRepositoryInterface $productRepository */ |
| 34 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 35 | +$productRepository->cleanCache(); |
| 36 | +/** @var ProductFactory $productFactory */ |
| 37 | +$productFactory = $objectManager->get(ProductFactory::class); |
| 38 | +$attributeValues = []; |
| 39 | +$associatedProductIds = []; |
| 40 | +$rootCategoryId = $baseWebsite->getDefaultStore()->getRootCategoryId(); |
| 41 | +array_shift($options); |
| 42 | + |
| 43 | +foreach ($options as $key => $option) { |
| 44 | + $product = $productFactory->create(); |
| 45 | + $product->setTypeId(ProductType::TYPE_SIMPLE) |
| 46 | + ->setAttributeSetId($product->getDefaultAttributeSetId()) |
| 47 | + ->setWebsiteIds([$key % 2 ? $baseWebsite->getId() : $secondWebsite->getId()]) |
| 48 | + ->setName('Configurable Option ' . $option->getLabel()) |
| 49 | + ->setSku(strtolower(str_replace(' ', '_', 'simple ' . $option->getLabel()))) |
| 50 | + ->setPrice(150) |
| 51 | + ->setTestConfigurable($option->getValue()) |
| 52 | + ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE) |
| 53 | + ->setStatus(Status::STATUS_ENABLED) |
| 54 | + ->setCategoryIds([$rootCategoryId]) |
| 55 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]); |
| 56 | + $product = $productRepository->save($product); |
| 57 | + |
| 58 | + $attributeValues[] = [ |
| 59 | + 'label' => 'test', |
| 60 | + 'attribute_id' => $attribute->getId(), |
| 61 | + 'value_index' => $option->getValue(), |
| 62 | + ]; |
| 63 | + $associatedProductIds[] = $product->getId(); |
| 64 | +} |
| 65 | +/** @var Factory $optionsFactory */ |
| 66 | +$optionsFactory = $objectManager->get(Factory::class); |
| 67 | +$configurableAttributesData = [ |
| 68 | + [ |
| 69 | + 'attribute_id' => $attribute->getId(), |
| 70 | + 'code' => $attribute->getAttributeCode(), |
| 71 | + 'label' => $attribute->getStoreLabel(), |
| 72 | + 'position' => '0', |
| 73 | + 'values' => $attributeValues, |
| 74 | + ], |
| 75 | +]; |
| 76 | +$configurableOptions = $optionsFactory->create($configurableAttributesData); |
| 77 | + |
| 78 | +$product = $productFactory->create(); |
| 79 | +/** @var ProductExtensionFactory $extensionAttributesFactory */ |
| 80 | +$extensionAttributesFactory = $objectManager->get(ProductExtensionFactory::class); |
| 81 | +$extensionConfigurableAttributes = $product->getExtensionAttributes() ?: $extensionAttributesFactory->create(); |
| 82 | +$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions); |
| 83 | +$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds); |
| 84 | +$product->setExtensionAttributes($extensionConfigurableAttributes); |
| 85 | + |
| 86 | +$product->setTypeId(Configurable::TYPE_CODE) |
| 87 | + ->setAttributeSetId($product->getDefaultAttributeSetId()) |
| 88 | + ->setWebsiteIds([$baseWebsite->getId(), $secondWebsite->getId()]) |
| 89 | + ->setName('Configurable Product') |
| 90 | + ->setSku('configurable') |
| 91 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 92 | + ->setStatus(Status::STATUS_ENABLED) |
| 93 | + ->setCategoryIds([$rootCategoryId]) |
| 94 | + ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]); |
| 95 | +$productRepository->save($product); |
0 commit comments