|
8 | 8 | use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory as CustomOptionFactory;
|
9 | 9 | use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory as ProductLinkFactory;
|
10 | 10 | use Magento\Catalog\Api\ProductRepositoryInterface\Proxy as ProductRepository;
|
| 11 | +use Magento\Catalog\Model\Product; |
11 | 12 | use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks;
|
12 | 13 | use Magento\Catalog\Model\Product\Link\Resolver as LinkResolver;
|
13 | 14 | use Magento\Framework\App\ObjectManager;
|
@@ -203,34 +204,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
|
203 | 204 | }
|
204 | 205 |
|
205 | 206 | $product = $this->setProductLinks($product);
|
206 |
| - |
207 |
| - /** |
208 |
| - * Initialize product options |
209 |
| - */ |
210 |
| - if ($productOptions && !$product->getOptionsReadonly()) { |
211 |
| - // mark custom options that should to fall back to default value |
212 |
| - $options = $this->mergeProductOptions( |
213 |
| - $productOptions, |
214 |
| - $this->request->getPost('options_use_default') |
215 |
| - ); |
216 |
| - $customOptions = []; |
217 |
| - foreach ($options as $customOptionData) { |
218 |
| - if (empty($customOptionData['is_delete'])) { |
219 |
| - if (empty($customOptionData['option_id'])) { |
220 |
| - $customOptionData['option_id'] = null; |
221 |
| - } |
222 |
| - if (isset($customOptionData['values'])) { |
223 |
| - $customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) { |
224 |
| - return empty($valueData['is_delete']); |
225 |
| - }); |
226 |
| - } |
227 |
| - $customOption = $this->customOptionFactory->create(['data' => $customOptionData]); |
228 |
| - $customOption->setProductSku($product->getSku()); |
229 |
| - $customOptions[] = $customOption; |
230 |
| - } |
231 |
| - } |
232 |
| - $product->setOptions($customOptions); |
233 |
| - } |
| 207 | + $product = $this->fillProductOptions($product, $productOptions); |
234 | 208 |
|
235 | 209 | $product->setCanSaveCustomOptions(
|
236 | 210 | !empty($productData['affect_product_custom_options']) && !$product->getOptionsReadonly()
|
@@ -427,4 +401,50 @@ private function filterWebsiteIds($websiteIds)
|
427 | 401 |
|
428 | 402 | return $websiteIds;
|
429 | 403 | }
|
| 404 | + |
| 405 | + /** |
| 406 | + * Fills $product with options from $productOptions array |
| 407 | + * |
| 408 | + * @param Product $product |
| 409 | + * @param array $productOptions |
| 410 | + * @return Product |
| 411 | + */ |
| 412 | + private function fillProductOptions(Product $product, array $productOptions) |
| 413 | + { |
| 414 | + if ($product->getOptionsReadonly()) { |
| 415 | + return $product; |
| 416 | + } |
| 417 | + |
| 418 | + if (empty($productOptions)) { |
| 419 | + return $product->setOptions([]); |
| 420 | + } |
| 421 | + |
| 422 | + // mark custom options that should to fall back to default value |
| 423 | + $options = $this->mergeProductOptions( |
| 424 | + $productOptions, |
| 425 | + $this->request->getPost('options_use_default') |
| 426 | + ); |
| 427 | + $customOptions = []; |
| 428 | + foreach ($options as $customOptionData) { |
| 429 | + if (!empty($customOptionData['is_delete'])) { |
| 430 | + continue; |
| 431 | + } |
| 432 | + |
| 433 | + if (empty($customOptionData['option_id'])) { |
| 434 | + $customOptionData['option_id'] = null; |
| 435 | + } |
| 436 | + |
| 437 | + if (isset($customOptionData['values'])) { |
| 438 | + $customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) { |
| 439 | + return empty($valueData['is_delete']); |
| 440 | + }); |
| 441 | + } |
| 442 | + |
| 443 | + $customOption = $this->customOptionFactory->create(['data' => $customOptionData]); |
| 444 | + $customOption->setProductSku($product->getSku()); |
| 445 | + $customOptions[] = $customOption; |
| 446 | + } |
| 447 | + |
| 448 | + return $product->setOptions($customOptions); |
| 449 | + } |
430 | 450 | }
|
0 commit comments