|
33 | 33 | ->setCategoryIds([2])
|
34 | 34 | ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
|
35 | 35 | ->save();
|
36 |
| - |
| 36 | +$productRepository = $objectManager->get(Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 37 | +/** |
| 38 | + * @var \Magento\Catalog\Model\Product $product |
| 39 | + */ |
37 | 40 | $product = $objectManager->create('Magento\Catalog\Model\Product');
|
38 | 41 | $product
|
39 | 42 | ->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)
|
|
96 | 99 | ]
|
97 | 100 | ],
|
98 | 101 | ]
|
99 |
| - ) |
| 102 | + )->setCustomAttributes([ |
| 103 | + "price_type" => [ |
| 104 | + 'attribute_code' => 'price_type', |
| 105 | + 'value' => \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC |
| 106 | + ], |
| 107 | + "price_view" => [ |
| 108 | + "attribute_code" => "price_view", |
| 109 | + "value" => "1", |
| 110 | + ], |
| 111 | + ]) |
100 | 112 | ->setCanSaveBundleSelections(true)
|
101 |
| - ->setAffectBundleProductSelections(true) |
102 |
| - ->save(); |
| 113 | + ->setHasOptions(false) |
| 114 | + ->setAffectBundleProductSelections(true); |
| 115 | +if ($product->getBundleOptionsData()) { |
| 116 | + $options = []; |
| 117 | + foreach ($product->getBundleOptionsData() as $key => $optionData) { |
| 118 | + if (!(bool)$optionData['delete']) { |
| 119 | + $option = $objectManager->create('Magento\Bundle\Api\Data\OptionInterfaceFactory') |
| 120 | + ->create(['data' => $optionData]); |
| 121 | + $option->setSku($product->getSku()); |
| 122 | + $option->setOptionId(null); |
103 | 123 |
|
104 |
| -//Load options |
105 |
| -$typeInstance = $product->getTypeInstance(); |
106 |
| -$typeInstance->setStoreFilter($product->getStoreId(), $product); |
107 |
| -$optionCollection = $typeInstance->getOptionsCollection($product); |
108 |
| -$selectionCollection = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product); |
| 124 | + $links = []; |
| 125 | + $bundleLinks = $product->getBundleSelectionsData(); |
| 126 | + if (!empty($bundleLinks[$key])) { |
| 127 | + foreach ($bundleLinks[$key] as $linkData) { |
| 128 | + if (!(bool)$linkData['delete']) { |
| 129 | + /** @var \Magento\Bundle\Api\Data\LinkInterface$link */ |
| 130 | + $link = $objectManager->create('Magento\Bundle\Api\Data\LinkInterfaceFactory') |
| 131 | + ->create(['data' => $linkData]); |
| 132 | + $linkProduct = $productRepository->getById($linkData['product_id']); |
| 133 | + $link->setSku($linkProduct->getSku()); |
| 134 | + $link->setQty($linkData['selection_qty']); |
| 135 | + if (isset($linkData['selection_can_change_qty'])) { |
| 136 | + $link->setCanChangeQuantity($linkData['selection_can_change_qty']); |
| 137 | + } |
| 138 | + $links[] = $link; |
| 139 | + } |
| 140 | + } |
| 141 | + $option->setProductLinks($links); |
| 142 | + $options[] = $option; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + $extension = $product->getExtensionAttributes(); |
| 147 | + $extension->setBundleProductOptions($options); |
| 148 | + $product->setExtensionAttributes($extension); |
| 149 | +} |
| 150 | +$productRepository->save($product); |
109 | 151 |
|
| 152 | +$product = $productRepository->get($product->getSku()); |
110 | 153 | $bundleOptions = [];
|
111 | 154 | $bundleOptionsQty = [];
|
112 | 155 | /** @var $option \Magento\Bundle\Model\Option */
|
113 |
| -foreach ($optionCollection as $option) { |
114 |
| - /** @var $selection \Magento\Bundle\Model\Selection */ |
115 |
| - foreach ($selectionCollection as $selection) { |
116 |
| - $bundleOptions[$option->getId()][] = $selection->getSelectionId(); |
| 156 | +foreach ($product->getExtensionAttributes()->getBundleProductOptions() as $option) { |
| 157 | + foreach ($option->getProductLinks() as $selection) { |
| 158 | + /** |
| 159 | + * @var \Magento\Bundle\Api\Data\LinkInterface $selection |
| 160 | + */ |
| 161 | + $bundleOptions[$option->getId()][] = $selection->getId(); |
117 | 162 | $bundleOptionsQty[$option->getId()][] = 1;
|
118 | 163 | }
|
119 | 164 | }
|
|
0 commit comments