|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +use Magento\TestFramework\Workaround\Override\Fixture\Resolver; |
| 8 | + |
| 9 | +/* |
| 10 | + * Since the bundle product creation GUI doesn't allow to choose values for bundled products' custom options, |
| 11 | + * bundled items should not contain products with required custom options. |
| 12 | + * However, if to create such a bundle product, it will be always out of stock. |
| 13 | + */ |
| 14 | +Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/products.php'); |
| 15 | + |
| 16 | +/** @var $objectManager \Magento\TestFramework\ObjectManager */ |
| 17 | +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 18 | +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ |
| 19 | +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 20 | +$sampleProduct = $productRepository->get('simple'); |
| 21 | + |
| 22 | +/** @var $product \Magento\Catalog\Model\Product */ |
| 23 | +$bundleProduct = $objectManager->create(\Magento\Catalog\Model\Product::class); |
| 24 | +$bundleProduct->setTypeId('bundle') |
| 25 | + ->setId(3) |
| 26 | + ->setAttributeSetId(4) |
| 27 | + ->setWeight(2) |
| 28 | + ->setWebsiteIds([1]) |
| 29 | + ->setName('Bundle Product') |
| 30 | + ->setSku('4bundle-product') |
| 31 | + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 32 | + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) |
| 33 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) |
| 34 | + ->setPriceView(1) |
| 35 | + ->setSkuType(1) |
| 36 | + ->setWeightType(1) |
| 37 | + ->setPriceType(1) |
| 38 | + ->setShipmentType(0) |
| 39 | + ->setPrice(10.0) |
| 40 | + ->setBundleOptionsData( |
| 41 | + [ |
| 42 | + [ |
| 43 | + 'title' => 'Bundle Product Items', |
| 44 | + 'default_title' => 'Bundle Product Items', |
| 45 | + 'type' => 'select', 'required' => 1, |
| 46 | + 'delete' => '', |
| 47 | + ], |
| 48 | + ] |
| 49 | + ) |
| 50 | + ->setBundleSelectionsData( |
| 51 | + [ |
| 52 | + [ |
| 53 | + [ |
| 54 | + 'product_id' => $sampleProduct->getId(), |
| 55 | + 'selection_price_value' => 2.75, |
| 56 | + 'selection_qty' => 1, |
| 57 | + 'selection_can_change_qty' => 1, |
| 58 | + 'delete' => '', |
| 59 | + |
| 60 | + ], |
| 61 | + ], |
| 62 | + ] |
| 63 | + ); |
| 64 | + |
| 65 | +/** @var $product \Magento\Catalog\Model\Product */ |
| 66 | +$bundleProduct2 = $objectManager->create(\Magento\Catalog\Model\Product::class); |
| 67 | +$bundleProduct2->setTypeId('bundle') |
| 68 | + ->setId(4) |
| 69 | + ->setAttributeSetId(4) |
| 70 | + ->setWeight(2) |
| 71 | + ->setWebsiteIds([1]) |
| 72 | + ->setName('Bundle Product 4') |
| 73 | + ->setSku('5bundle-product') |
| 74 | + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 75 | + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) |
| 76 | + ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) |
| 77 | + ->setPriceView(1) |
| 78 | + ->setSkuType(1) |
| 79 | + ->setWeightType(1) |
| 80 | + ->setPriceType(1) |
| 81 | + ->setShipmentType(0) |
| 82 | + ->setPrice(10.0) |
| 83 | + ->setBundleOptionsData( |
| 84 | + [ |
| 85 | + [ |
| 86 | + 'title' => 'Bundle Product Items', |
| 87 | + 'default_title' => 'Bundle Product Items', |
| 88 | + 'type' => 'select', 'required' => 1, |
| 89 | + 'delete' => '', |
| 90 | + ], |
| 91 | + ] |
| 92 | + ) |
| 93 | + ->setBundleSelectionsData( |
| 94 | + [ |
| 95 | + [ |
| 96 | + [ |
| 97 | + 'product_id' => $sampleProduct->getId(), |
| 98 | + 'selection_price_value' => 2.75, |
| 99 | + 'selection_qty' => 1, |
| 100 | + 'selection_can_change_qty' => 1, |
| 101 | + 'delete' => '', |
| 102 | + |
| 103 | + ], |
| 104 | + ], |
| 105 | + ] |
| 106 | + ); |
| 107 | + |
| 108 | +foreach ([$bundleProduct, $bundleProduct2] as $product) { |
| 109 | + if ($product->getBundleOptionsData()) { |
| 110 | + $options = []; |
| 111 | + foreach ($product->getBundleOptionsData() as $key => $optionData) { |
| 112 | + if (!(bool)$optionData['delete']) { |
| 113 | + $option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class) |
| 114 | + ->create(['data' => $optionData]); |
| 115 | + $option->setSku($product->getSku()); |
| 116 | + $option->setOptionId(null); |
| 117 | + |
| 118 | + $links = []; |
| 119 | + $bundleLinks = $product->getBundleSelectionsData(); |
| 120 | + if (!empty($bundleLinks[$key])) { |
| 121 | + foreach ($bundleLinks[$key] as $linkData) { |
| 122 | + if (!(bool)$linkData['delete']) { |
| 123 | + /** @var \Magento\Bundle\Api\Data\LinkInterface$link */ |
| 124 | + $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class) |
| 125 | + ->create(['data' => $linkData]); |
| 126 | + $linkProduct = $productRepository->getById($linkData['product_id']); |
| 127 | + $link->setSku($linkProduct->getSku()); |
| 128 | + $link->setQty($linkData['selection_qty']); |
| 129 | + $link->setPrice($linkData['selection_price_value']); |
| 130 | + if (isset($linkData['selection_can_change_qty'])) { |
| 131 | + $link->setCanChangeQuantity($linkData['selection_can_change_qty']); |
| 132 | + } |
| 133 | + $links[] = $link; |
| 134 | + } |
| 135 | + } |
| 136 | + $option->setProductLinks($links); |
| 137 | + $options[] = $option; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + $extension = $product->getExtensionAttributes(); |
| 142 | + $extension->setBundleProductOptions($options); |
| 143 | + $product->setExtensionAttributes($extension); |
| 144 | + } |
| 145 | + |
| 146 | + $productRepository->save($product, true); |
| 147 | +} |
0 commit comments