|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 8 | +use Magento\Catalog\Model\Product; |
| 9 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 10 | +use Magento\Catalog\Model\Product\Type; |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\Catalog\Setup\CategorySetup; |
| 13 | +use Magento\ConfigurableProduct\Helper\Product\Options\Factory; |
| 14 | +use Magento\ConfigurableProduct\Model\Product\Type\Configurable; |
| 15 | +use Magento\Eav\Api\Data\AttributeOptionInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | + |
| 18 | +\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize(); |
| 19 | + |
| 20 | +require __DIR__ . '/configurable_attribute.php'; |
| 21 | + |
| 22 | +/** @var ProductRepositoryInterface $productRepository */ |
| 23 | +$productRepository = Bootstrap::getObjectManager() |
| 24 | + ->create(ProductRepositoryInterface::class); |
| 25 | + |
| 26 | +/** @var $installer CategorySetup */ |
| 27 | +$installer = Bootstrap::getObjectManager()->create(CategorySetup::class); |
| 28 | + |
| 29 | +/* Create simple products per each option value*/ |
| 30 | +/** @var AttributeOptionInterface[] $options */ |
| 31 | +$options = $attribute->getOptions(); |
| 32 | + |
| 33 | +$attributeValues = []; |
| 34 | +$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default'); |
| 35 | +$associatedProductIds = []; |
| 36 | +$productIds = [30, 40]; |
| 37 | +array_shift($options); //remove the first option which is empty |
| 38 | + |
| 39 | +foreach ($options as $option) { |
| 40 | + /** @var $product Product */ |
| 41 | + $product = Bootstrap::getObjectManager()->create(Product::class); |
| 42 | + $productId = array_shift($productIds); |
| 43 | + $product->setTypeId(Type::TYPE_SIMPLE) |
| 44 | + ->setId($productId) |
| 45 | + ->setAttributeSetId($attributeSetId) |
| 46 | + ->setWebsiteIds([1]) |
| 47 | + ->setName('Configurable Option' . $option->getLabel()) |
| 48 | + ->setSku('simple_' . $productId) |
| 49 | + ->setPrice($productId) |
| 50 | + ->setTestConfigurable($option->getValue()) |
| 51 | + ->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE) |
| 52 | + ->setStatus(Status::STATUS_ENABLED) |
| 53 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]); |
| 54 | + |
| 55 | + $product = $productRepository->save($product); |
| 56 | + |
| 57 | + /** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */ |
| 58 | + $stockItem = Bootstrap::getObjectManager()->create(\Magento\CatalogInventory\Model\Stock\Item::class); |
| 59 | + $stockItem->load($productId, 'product_id'); |
| 60 | + |
| 61 | + if (!$stockItem->getProductId()) { |
| 62 | + $stockItem->setProductId($productId); |
| 63 | + } |
| 64 | + $stockItem->setUseConfigManageStock(1); |
| 65 | + $stockItem->setQty(1000); |
| 66 | + $stockItem->setIsQtyDecimal(0); |
| 67 | + $stockItem->setIsInStock(1); |
| 68 | + $stockItem->save(); |
| 69 | + |
| 70 | + $attributeValues[] = [ |
| 71 | + 'label' => 'test', |
| 72 | + 'attribute_id' => $attribute->getId(), |
| 73 | + 'value_index' => $option->getValue(), |
| 74 | + ]; |
| 75 | + $associatedProductIds[] = $product->getId(); |
| 76 | +} |
| 77 | + |
| 78 | +/** @var $product Product */ |
| 79 | +$product = Bootstrap::getObjectManager()->create(Product::class); |
| 80 | + |
| 81 | +/** @var Factory $optionsFactory */ |
| 82 | +$optionsFactory = Bootstrap::getObjectManager()->create(Factory::class); |
| 83 | + |
| 84 | +$configurableAttributesData = [ |
| 85 | + [ |
| 86 | + 'attribute_id' => $attribute->getId(), |
| 87 | + 'code' => $attribute->getAttributeCode(), |
| 88 | + 'label' => $attribute->getStoreLabel(), |
| 89 | + 'position' => '0', |
| 90 | + 'values' => $attributeValues, |
| 91 | + ], |
| 92 | +]; |
| 93 | + |
| 94 | +$configurableOptions = $optionsFactory->create($configurableAttributesData); |
| 95 | + |
| 96 | +$extensionConfigurableAttributes = $product->getExtensionAttributes(); |
| 97 | +$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions); |
| 98 | +$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds); |
| 99 | + |
| 100 | +$product->setExtensionAttributes($extensionConfigurableAttributes); |
| 101 | + |
| 102 | +// Remove any previously created product with the same id. |
| 103 | +/** @var \Magento\Framework\Registry $registry */ |
| 104 | +$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class); |
| 105 | +$registry->unregister('isSecureArea'); |
| 106 | +$registry->register('isSecureArea', true); |
| 107 | +try { |
| 108 | + $productToDelete = $productRepository->getById(11); |
| 109 | + $productRepository->delete($productToDelete); |
| 110 | + |
| 111 | + /** @var \Magento\Quote\Model\ResourceModel\Quote\Item $itemResource */ |
| 112 | + $itemResource = Bootstrap::getObjectManager()->get(\Magento\Quote\Model\ResourceModel\Quote\Item::class); |
| 113 | + $itemResource->getConnection()->delete( |
| 114 | + $itemResource->getMainTable(), |
| 115 | + 'product_id = ' . $productToDelete->getId() |
| 116 | + ); |
| 117 | +} catch (\Exception $e) { |
| 118 | + // Nothing to remove |
| 119 | +} |
| 120 | +$registry->unregister('isSecureArea'); |
| 121 | +$registry->register('isSecureArea', false); |
| 122 | + |
| 123 | +$product->setTypeId(Configurable::TYPE_CODE) |
| 124 | + ->setId(11) |
| 125 | + ->setAttributeSetId($attributeSetId) |
| 126 | + ->setWebsiteIds([1]) |
| 127 | + ->setName('Configurable Product 12345') |
| 128 | + ->setSku('12345') |
| 129 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 130 | + ->setStatus(Status::STATUS_ENABLED) |
| 131 | + ->setStockData(['use_config_manage_stock' => 1, 'is_in_stock' => 1]); |
| 132 | + |
| 133 | +$productRepository->save($product); |
0 commit comments