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