|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Bundle\Model; |
| 7 | + |
| 8 | +use Magento\Bundle\Api\Data\BundleOptionInterface; |
| 9 | +use Magento\Bundle\Api\Data\BundleOptionInterfaceFactory; |
| 10 | +use Magento\Catalog\Api\Data\ProductOptionInterface; |
| 11 | +use Magento\Catalog\Model\Product\Type as ProductType; |
| 12 | +use Magento\Catalog\Model\ProductOptionProcessorInterface; |
| 13 | +use Magento\Framework\DataObject; |
| 14 | +use Magento\Framework\DataObject\Factory as DataObjectFactory; |
| 15 | + |
| 16 | +class ProductOptionProcessor implements ProductOptionProcessorInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var DataObjectFactory |
| 20 | + */ |
| 21 | + protected $objectFactory; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var BundleOptionInterfaceFactory |
| 25 | + */ |
| 26 | + protected $bundleOptionFactory; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param DataObjectFactory $objectFactory |
| 30 | + * @param BundleOptionInterfaceFactory $bundleOptionFactory |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + DataObjectFactory $objectFactory, |
| 34 | + BundleOptionInterfaceFactory $bundleOptionFactory |
| 35 | + ) { |
| 36 | + $this->objectFactory = $objectFactory; |
| 37 | + $this->bundleOptionFactory = $bundleOptionFactory; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + public function convertToBuyRequest(ProductOptionInterface $productOption) |
| 44 | + { |
| 45 | + /** @var DataObject $request */ |
| 46 | + $request = $this->objectFactory->create(); |
| 47 | + |
| 48 | + $bundleOptions = $this->getBundleOptions($productOption); |
| 49 | + if (!empty($bundleOptions) && is_array($bundleOptions)) { |
| 50 | + $requestData = []; |
| 51 | + foreach ($bundleOptions as $option) { |
| 52 | + /** @var BundleOptionInterface $option */ |
| 53 | + foreach ($option->getOptionSelections() as $selection) { |
| 54 | + $requestData['bundle_option'][$option->getOptionId()][] = $selection; |
| 55 | + $requestData['bundle_option_qty'][$option->getOptionId()] = $option->getOptionQty(); |
| 56 | + } |
| 57 | + } |
| 58 | + $request->addData($requestData); |
| 59 | + } |
| 60 | + |
| 61 | + return $request; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Retrieve bundle options |
| 66 | + * |
| 67 | + * @param ProductOptionInterface $productOption |
| 68 | + * @return array |
| 69 | + */ |
| 70 | + protected function getBundleOptions(ProductOptionInterface $productOption) |
| 71 | + { |
| 72 | + if ($productOption |
| 73 | + && $productOption->getExtensionAttributes() |
| 74 | + && $productOption->getExtensionAttributes()->getBundleOptions() |
| 75 | + ) { |
| 76 | + return $productOption->getExtensionAttributes() |
| 77 | + ->getBundleOptions(); |
| 78 | + } |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritdoc} |
| 84 | + */ |
| 85 | + public function convertToProductOption(DataObject $request) |
| 86 | + { |
| 87 | + $bundleOptions = $request->getBundleOption(); |
| 88 | + $bundleOptionsQty = $request->getBundleOptionQty(); |
| 89 | + |
| 90 | + if (!empty($bundleOptions) && is_array($bundleOptions)) { |
| 91 | + $data = []; |
| 92 | + foreach ($bundleOptions as $optionId => $optionSelections) { |
| 93 | + if (empty($optionSelections)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + $optionSelections = is_array($optionSelections) ? $optionSelections : [$optionSelections]; |
| 97 | + $optionQty = isset($bundleOptionsQty[$optionId]) ? $bundleOptionsQty[$optionId] : 1; |
| 98 | + |
| 99 | + /** @var BundleOptionInterface $productOption */ |
| 100 | + $productOption = $this->bundleOptionFactory->create(); |
| 101 | + $productOption->setOptionId($optionId); |
| 102 | + $productOption->setOptionSelections($optionSelections); |
| 103 | + $productOption->setOptionQty($optionQty); |
| 104 | + $data[] = $productOption; |
| 105 | + } |
| 106 | + |
| 107 | + return ['bundle_options' => $data]; |
| 108 | + } |
| 109 | + |
| 110 | + return []; |
| 111 | + } |
| 112 | +} |
0 commit comments