3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \GroupedProduct \Model \Quote \Item ;
8
9
10
+ use Magento \Framework \DataObject ;
9
11
use Magento \Framework \DataObject \Factory as ObjectFactory ;
12
+ use Magento \Framework \Serialize \Serializer \Json ;
13
+ use Magento \GroupedProduct \Api \Data \GroupedOptionsInterface ;
14
+ use Magento \GroupedProduct \Api \Data \GroupedOptionsInterfaceFactory ;
15
+ use Magento \GroupedProduct \Model \Product \Type \Grouped ;
16
+ use Magento \Quote \Api \Data as QuoteApi ;
10
17
use Magento \Quote \Api \Data \CartItemInterface ;
11
- use Magento \Quote \Api \Data \ProductOptionInterface ;
12
18
use Magento \Quote \Model \Quote \Item \CartItemProcessorInterface ;
13
19
14
20
/**
15
- * A class that converts the Grouped Product super group, as received over RESTful API,
16
- * into the format needed within the buy request
17
- *
18
- * Class \Magento\GroupedProduct\Model\Quote\Item\CartItemProcessor
21
+ * Converts grouped_options to super_group for the grouped product.
19
22
*/
20
23
class CartItemProcessor implements CartItemProcessorInterface
21
24
{
25
+ private const SUPER_GROUP_CODE = 'super_group ' ;
26
+
22
27
/**
23
28
* @var ObjectFactory
24
29
*/
25
30
private $ objectFactory ;
26
31
27
32
/**
28
- * CartItemProcessor constructor.
29
- *
33
+ * @var GroupedOptionsInterface
34
+ */
35
+ private $ groupedOptionFactory ;
36
+
37
+ /**
38
+ * @var Json
39
+ */
40
+ private $ jsonSerializer ;
41
+
42
+ /**
43
+ * @var QuoteApi\ProductOptionExtensionFactory
44
+ */
45
+ private $ productOptionExtensionFactory ;
46
+
47
+ /**
48
+ * @var QuoteApi\ProductOptionInterfaceFactory
49
+ */
50
+ private $ productOptionFactory ;
51
+
52
+ /**
30
53
* @param ObjectFactory $objectFactory
54
+ * @param GroupedOptionsInterfaceFactory $groupedOptionFactory
55
+ * @param Json $jsonSerializer
56
+ * @param QuoteApi\ProductOptionExtensionFactory $productOptionExtensionFactory
57
+ * @param QuoteApi\ProductOptionInterfaceFactory $productOptionFactory
31
58
*/
32
- public function __construct (ObjectFactory $ objectFactory )
33
- {
59
+ public function __construct (
60
+ ObjectFactory $ objectFactory ,
61
+ GroupedOptionsInterfaceFactory $ groupedOptionFactory ,
62
+ Json $ jsonSerializer ,
63
+ QuoteApi \ProductOptionExtensionFactory $ productOptionExtensionFactory ,
64
+ QuoteApi \ProductOptionInterfaceFactory $ productOptionFactory
65
+ ) {
34
66
$ this ->objectFactory = $ objectFactory ;
67
+ $ this ->groupedOptionFactory = $ groupedOptionFactory ;
68
+ $ this ->jsonSerializer = $ jsonSerializer ;
69
+ $ this ->productOptionExtensionFactory = $ productOptionExtensionFactory ;
70
+ $ this ->productOptionFactory = $ productOptionFactory ;
35
71
}
36
72
37
73
/**
38
- * Converts the super_group request data into the same format as native frontend add-to-cart
74
+ * Converts the grouped_options request data into the same format as native frontend add-to-cart
39
75
*
40
76
* @param CartItemInterface $cartItem
41
- *
42
- * @return \Magento\Framework\DataObject|null
77
+ * @return DataObject|null
43
78
*/
44
- public function convertToBuyRequest (CartItemInterface $ cartItem )
79
+ public function convertToBuyRequest (CartItemInterface $ cartItem ): ? DataObject
45
80
{
46
- $ productOption = $ cartItem ->getProductOption ();
47
- if ($ productOption instanceof ProductOptionInterface && $ productOption ->getExtensionAttributes ()) {
48
- $ superGroup = $ productOption ->getExtensionAttributes ()->getSuperGroup ();
49
- if (is_array ($ superGroup )) {
81
+ $ extensionAttributes = $ cartItem ->getProductOption ()
82
+ ? $ cartItem ->getProductOption ()->getExtensionAttributes ()
83
+ : null ;
84
+ if ($ extensionAttributes ) {
85
+ $ groupedOptions = $ extensionAttributes ->getGroupedOptions ();
86
+ if ($ groupedOptions ) {
50
87
$ requestData = [];
51
88
52
- /** @var GroupedItemQty $item */
53
- foreach ($ superGroup as $ item ) {
54
- if (!isset ($ requestData ['super_group ' ])) {
55
- $ requestData ['super_group ' ] = [];
56
- }
57
-
58
- $ requestData ['super_group ' ][$ item ->getProductId ()] = $ item ->getQty ();
89
+ foreach ($ groupedOptions as $ item ) {
90
+ $ requestData [self ::SUPER_GROUP_CODE ][$ item ->getId ()] = $ item ->getQty ();
59
91
}
60
92
61
- if (!empty ($ requestData )) {
62
- return $ this ->objectFactory ->create ($ requestData );
63
- }
93
+ return $ this ->objectFactory ->create ($ requestData );
64
94
}
65
95
}
66
96
@@ -71,11 +101,29 @@ public function convertToBuyRequest(CartItemInterface $cartItem)
71
101
* Option processor
72
102
*
73
103
* @param CartItemInterface $cartItem
74
- *
75
104
* @return CartItemInterface
76
105
*/
77
- public function processOptions (CartItemInterface $ cartItem )
106
+ public function processOptions (CartItemInterface $ cartItem ): CartItemInterface
78
107
{
108
+ if ($ cartItem ->getProductType () !== Grouped::TYPE_CODE ) {
109
+ return $ cartItem ;
110
+ }
111
+
112
+ $ superGroup = $ cartItem ->getOptionByCode (self ::SUPER_GROUP_CODE );
113
+ $ superGroupValues = $ superGroup ? $ this ->jsonSerializer ->unserialize ($ superGroup ->getValue ()) : null ;
114
+ if ($ superGroupValues ) {
115
+ $ productOptions = [];
116
+ foreach ($ superGroupValues as $ id => $ qty ) {
117
+ $ productOptions [] = $ this ->groupedOptionFactory ->create (['id ' => $ id , 'qty ' => $ qty ]);
118
+ }
119
+
120
+ $ extension = $ this ->productOptionExtensionFactory ->create ()->setGroupedOptions ($ productOptions );
121
+ if (!$ cartItem ->getProductOption ()) {
122
+ $ cartItem ->setProductOption ($ this ->productOptionFactory ->create ());
123
+ }
124
+ $ cartItem ->getProductOption ()->setExtensionAttributes ($ extension );
125
+ }
126
+
79
127
return $ cartItem ;
80
128
}
81
129
}
0 commit comments