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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\CustomOptions;
use Magento\Framework\DataObject;
use Magento\Quote\Api\Data\CartItemInterface;
use Magento\Quote\Model\Quote\Item\CartItemProcessorInterface;
use Magento\Quote\Api\Data\ProductOptionExtensionFactory;
use Magento\Quote\Model\Quote\ProductOptionFactory;
class CustomOptionProcessor implements CartItemProcessorInterface
{
/**
* @var \Magento\Framework\DataObject\Factory
*/
protected $objectFactory;
/**
* @var \Magento\Quote\Model\Quote\ProductOptionFactory
*/
protected $productOptionFactory;
/**
* @var \Magento\Quote\Api\Data\ProductOptionExtensionFactory
*/
protected $extensionFactory;
/**
* @var \Magento\Catalog\Model\CustomOptions\CustomOptionFactory
*/
protected $customOptionFactory;
/**
* @var \Magento\Catalog\Model\Product\Option\UrlBuilder
*/
private $urlBuilder;
/**
* Serializer interface instance.
*
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;
/**
* @param DataObject\Factory $objectFactory
* @param ProductOptionFactory $productOptionFactory
* @param ProductOptionExtensionFactory $extensionFactory
* @param CustomOptionFactory $customOptionFactory
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
*/
public function __construct(
\Magento\Framework\DataObject\Factory $objectFactory,
\Magento\Quote\Model\Quote\ProductOptionFactory $productOptionFactory,
\Magento\Quote\Api\Data\ProductOptionExtensionFactory $extensionFactory,
\Magento\Catalog\Model\CustomOptions\CustomOptionFactory $customOptionFactory,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->objectFactory = $objectFactory;
$this->productOptionFactory = $productOptionFactory;
$this->extensionFactory = $extensionFactory;
$this->customOptionFactory = $customOptionFactory;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}
/**
* @inheritDoc
*/
public function convertToBuyRequest(CartItemInterface $cartItem)
{
if ($cartItem->getProductOption()
&& $cartItem->getProductOption()->getExtensionAttributes()
&& $cartItem->getProductOption()->getExtensionAttributes()->getCustomOptions()) {
$customOptions = $cartItem->getProductOption()->getExtensionAttributes()->getCustomOptions();
if (!empty($customOptions) && is_array($customOptions)) {
$requestData = [];
foreach ($customOptions as $option) {
$requestData['options'][$option->getOptionId()] = $option->getOptionValue();
}
return $this->objectFactory->create($requestData);
}
}
return null;
}
/**
* @inheritDoc
*/
public function processOptions(CartItemInterface $cartItem)
{
$options = $this->getOptions($cartItem);
if (!empty($options) && is_array($options)) {
$this->updateOptionsValues($options);
$productOption = $cartItem->getProductOption()
? $cartItem->getProductOption()
: $this->productOptionFactory->create();
/** @var \Magento\Quote\Api\Data\ProductOptionExtensionInterface $extensibleAttribute */
$extensibleAttribute = $productOption->getExtensionAttributes()
? $productOption->getExtensionAttributes()
: $this->extensionFactory->create();
$extensibleAttribute->setCustomOptions($options);
$productOption->setExtensionAttributes($extensibleAttribute);
$cartItem->setProductOption($productOption);
}
return $cartItem;
}
/**
* Receive custom option from buy request
*
* @param CartItemInterface $cartItem
* @return array
*/
protected function getOptions(CartItemInterface $cartItem)
{
$buyRequest = !empty($cartItem->getOptionByCode('info_buyRequest'))
? $this->serializer->unserialize($cartItem->getOptionByCode('info_buyRequest')->getValue())
: null;
return is_array($buyRequest) && isset($buyRequest['options'])
? $buyRequest['options']
: [];
}
/**
* Update options values
*
* @param array $options
* @return null
*/
protected function updateOptionsValues(array &$options)
{
foreach ($options as $optionId => &$optionValue) {
/** @var \Magento\Catalog\Model\CustomOptions\CustomOption $option */
$option = $this->customOptionFactory->create();
$option->setOptionId($optionId);
if (is_array($optionValue)) {
$optionValue = $this->processFileOptionValue($optionValue);
$optionValue = $this->processDateOptionValue($optionValue);
$optionValue = implode(',', $optionValue);
}
$option->setOptionValue($optionValue);
$optionValue = $option;
}
}
/**
* Returns option value with file built URL
*
* @param array $optionValue
* @return array
*/
private function processFileOptionValue(array $optionValue)
{
if (array_key_exists('url', $optionValue) &&
array_key_exists('route', $optionValue['url']) &&
array_key_exists('params', $optionValue['url'])
) {
$optionValue['url'] = $this->getUrlBuilder()->getUrl(
$optionValue['url']['route'],
$optionValue['url']['params']
);
}
return $optionValue;
}
/**
* Returns date option value only with 'date_internal data
*
* @param array $optionValue
* @return array
*/
private function processDateOptionValue(array $optionValue)
{
if (array_key_exists('date_internal', $optionValue)
) {
$closure = function ($key) {
return $key === 'date_internal';
};
$optionValue = array_filter($optionValue, $closure, ARRAY_FILTER_USE_KEY);
}
return $optionValue;
}
/**
* @return \Magento\Catalog\Model\Product\Option\UrlBuilder
*
* @deprecated 101.0.0
*/
private function getUrlBuilder()
{
if ($this->urlBuilder === null) {
$this->urlBuilder = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\Option\UrlBuilder::class);
}
return $this->urlBuilder;
}
}