3
3
* Copyright 2015 Adobe
4
4
* All Rights Reserved.
5
5
*/
6
+
6
7
namespace Magento \Catalog \Model \CustomOptions ;
7
8
9
+ use Magento \Catalog \Api \Data \CustomOptionInterface ;
10
+ use Magento \Catalog \Api \Data \ProductCustomOptionInterface ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
12
+ use Magento \Catalog \Model \Product \Option \Type \File \ImageContentProcessor ;
8
13
use Magento \Framework \DataObject ;
14
+ use Magento \Framework \Exception \NoSuchEntityException ;
9
15
use Magento \Quote \Api \Data \CartItemInterface ;
10
16
use Magento \Quote \Model \Quote \Item \CartItemProcessorInterface ;
11
17
use Magento \Quote \Api \Data \ProductOptionExtensionFactory ;
12
18
use Magento \Quote \Model \Quote \ProductOptionFactory ;
13
19
20
+ /**
21
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
+ */
14
23
class CustomOptionProcessor implements CartItemProcessorInterface
15
24
{
16
25
/**
@@ -45,41 +54,63 @@ class CustomOptionProcessor implements CartItemProcessorInterface
45
54
*/
46
55
private $ serializer ;
47
56
57
+ /**
58
+ * @var ProductRepositoryInterface
59
+ */
60
+ private $ productRepository ;
61
+
62
+ /**
63
+ * @var ImageContentProcessor
64
+ */
65
+ private $ imageContentProcessor ;
66
+
48
67
/**
49
68
* @param DataObject\Factory $objectFactory
50
69
* @param ProductOptionFactory $productOptionFactory
51
70
* @param ProductOptionExtensionFactory $extensionFactory
52
71
* @param CustomOptionFactory $customOptionFactory
53
72
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
73
+ * @param ProductRepositoryInterface|null $productRepository
74
+ * @param ImageContentProcessor|null $imageContentProcessor
54
75
*/
55
76
public function __construct (
56
77
\Magento \Framework \DataObject \Factory $ objectFactory ,
57
78
\Magento \Quote \Model \Quote \ProductOptionFactory $ productOptionFactory ,
58
79
\Magento \Quote \Api \Data \ProductOptionExtensionFactory $ extensionFactory ,
59
80
\Magento \Catalog \Model \CustomOptions \CustomOptionFactory $ customOptionFactory ,
60
- ?\Magento \Framework \Serialize \Serializer \Json $ serializer = null
81
+ ?\Magento \Framework \Serialize \Serializer \Json $ serializer = null ,
82
+ ?ProductRepositoryInterface $ productRepository = null ,
83
+ ?ImageContentProcessor $ imageContentProcessor = null
61
84
) {
62
85
$ this ->objectFactory = $ objectFactory ;
63
86
$ this ->productOptionFactory = $ productOptionFactory ;
64
87
$ this ->extensionFactory = $ extensionFactory ;
65
88
$ this ->customOptionFactory = $ customOptionFactory ;
66
89
$ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()
67
90
->get (\Magento \Framework \Serialize \Serializer \Json::class);
91
+ $ this ->productRepository = $ productRepository
92
+ ?: \Magento \Framework \App \ObjectManager::getInstance ()
93
+ ->get (ProductRepositoryInterface::class);
94
+ $ this ->imageContentProcessor = $ imageContentProcessor
95
+ ?: \Magento \Framework \App \ObjectManager::getInstance ()
96
+ ->get (ImageContentProcessor::class);
68
97
}
69
98
70
99
/**
71
100
* @inheritDoc
72
101
*/
73
102
public function convertToBuyRequest (CartItemInterface $ cartItem )
74
103
{
75
- if ($ cartItem ->getProductOption ()
76
- && $ cartItem ->getProductOption ()->getExtensionAttributes ()
77
- && $ cartItem ->getProductOption ()->getExtensionAttributes ()->getCustomOptions ()) {
104
+ if ($ cartItem ->getProductOption ()?->getExtensionAttributes()?->getCustomOptions()) {
78
105
$ customOptions = $ cartItem ->getProductOption ()->getExtensionAttributes ()->getCustomOptions ();
79
- if (!empty ($ customOptions ) && is_array ( $ customOptions ) ) {
106
+ if (!empty ($ customOptions )) {
80
107
$ requestData = [];
108
+ $ productOptions = $ this ->getProductCustomOptions ($ cartItem );
81
109
foreach ($ customOptions as $ option ) {
82
- $ requestData ['options ' ][$ option ->getOptionId ()] = $ option ->getOptionValue ();
110
+ $ requestData ['options ' ][$ option ->getOptionId ()] = $ this ->getCustomOptionValue (
111
+ $ option ,
112
+ $ productOptions [$ option ->getOptionId ()] ?? null
113
+ );
83
114
}
84
115
return $ this ->objectFactory ->create ($ requestData );
85
116
}
@@ -99,7 +130,6 @@ public function processOptions(CartItemInterface $cartItem)
99
130
? $ cartItem ->getProductOption ()
100
131
: $ this ->productOptionFactory ->create ();
101
132
102
- /** @var \Magento\Quote\Api\Data\ProductOptionExtensionInterface $extensibleAttribute */
103
133
$ extensibleAttribute = $ productOption ->getExtensionAttributes ()
104
134
? $ productOption ->getExtensionAttributes ()
105
135
: $ this ->extensionFactory ->create ();
@@ -136,7 +166,7 @@ protected function getOptions(CartItemInterface $cartItem)
136
166
protected function updateOptionsValues (array &$ options )
137
167
{
138
168
foreach ($ options as $ optionId => &$ optionValue ) {
139
- /** @var \Magento\Catalog\Model\CustomOptions\ CustomOption $option */
169
+ /** @var CustomOption $option */
140
170
$ option = $ this ->customOptionFactory ->create ();
141
171
$ option ->setOptionId ($ optionId );
142
172
if (is_array ($ optionValue )) {
@@ -204,4 +234,55 @@ private function getUrlBuilder()
204
234
}
205
235
return $ this ->urlBuilder ;
206
236
}
237
+
238
+ /**
239
+ * Get Product Options
240
+ *
241
+ * @param CartItemInterface $cartItem
242
+ * @return ProductCustomOptionInterface[]
243
+ */
244
+ private function getProductCustomOptions (CartItemInterface $ cartItem ): array
245
+ {
246
+ try {
247
+ $ product = $ this ->productRepository ->get ($ cartItem ->getSku ());
248
+ } catch (NoSuchEntityException ) {
249
+ $ product = null ;
250
+ }
251
+
252
+ $ options = [];
253
+ foreach ($ product ?->getHasOptions() ? $ product ->getOptions () : [] as $ option ) {
254
+ $ options [$ option ->getOptionId ()] = $ option ;
255
+ }
256
+ return $ options ;
257
+ }
258
+
259
+ /**
260
+ * Get custom option value depending on the type of custom option
261
+ *
262
+ * @param CustomOptionInterface $customOption
263
+ * @param ProductCustomOptionInterface|null $productCustomOption
264
+ * @return string|array|null
265
+ */
266
+ private function getCustomOptionValue (
267
+ CustomOptionInterface $ customOption ,
268
+ ?ProductCustomOptionInterface $ productCustomOption = null
269
+ ): mixed {
270
+ if ($ customOption ->getExtensionAttributes ()?->getFileInfo()) {
271
+ if ($ productCustomOption
272
+ && $ productCustomOption ->getType () === ProductCustomOptionInterface::OPTION_TYPE_FILE
273
+ ) {
274
+ return $ this ->imageContentProcessor ->process (
275
+ $ customOption ->getExtensionAttributes ()->getFileInfo (),
276
+ $ productCustomOption
277
+ );
278
+ } elseif ($ customOption instanceof CustomOption) {
279
+ // Check if the custom option is an instance of CustomOption for backward compatibility
280
+ // Bypass CustomOption::getOptionValue as the current implementation would process the file
281
+ // even if it is not a file option.
282
+ return $ customOption ->getData (CustomOptionInterface::OPTION_VALUE );
283
+ }
284
+ }
285
+
286
+ return $ customOption ->getOptionValue ();
287
+ }
207
288
}
0 commit comments