15
15
use Magento \Framework \GraphQl \Query \ResolverInterface ;
16
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
17
17
use Magento \Quote \Api \CartItemRepositoryInterface ;
18
+ use Magento \Quote \Api \CartRepositoryInterface ;
18
19
use Magento \Quote \Model \Quote ;
19
20
use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
21
+ use Magento \QuoteGraphQl \Model \Cart \UpdateCartItem ;
20
22
21
23
/**
22
24
* @inheritdoc
23
25
*/
24
26
class UpdateCartItems implements ResolverInterface
25
27
{
28
+ /**
29
+ * @var CartRepositoryInterface
30
+ */
31
+ private $ quoteRepository ;
32
+
33
+ /**
34
+ * @var UpdateCartItem
35
+ */
36
+ private $ updateCartItem ;
37
+
26
38
/**
27
39
* @var GetCartForUser
28
40
*/
@@ -36,13 +48,19 @@ class UpdateCartItems implements ResolverInterface
36
48
/**
37
49
* @param GetCartForUser $getCartForUser
38
50
* @param CartItemRepositoryInterface $cartItemRepository
51
+ * @param UpdateCartItem $updateCartItem
52
+ * @param CartRepositoryInterface $quoteRepository
39
53
*/
40
54
public function __construct (
41
55
GetCartForUser $ getCartForUser ,
42
- CartItemRepositoryInterface $ cartItemRepository
56
+ CartItemRepositoryInterface $ cartItemRepository ,
57
+ UpdateCartItem $ updateCartItem ,
58
+ CartRepositoryInterface $ quoteRepository
43
59
) {
44
60
$ this ->getCartForUser = $ getCartForUser ;
45
61
$ this ->cartItemRepository = $ cartItemRepository ;
62
+ $ this ->updateCartItem = $ updateCartItem ;
63
+ $ this ->quoteRepository = $ quoteRepository ;
46
64
}
47
65
48
66
/**
@@ -93,26 +111,47 @@ private function processCartItems(Quote $cart, array $items): void
93
111
if (!isset ($ item ['cart_item_id ' ]) || empty ($ item ['cart_item_id ' ])) {
94
112
throw new GraphQlInputException (__ ('Required parameter "cart_item_id" for "cart_items" is missing. ' ));
95
113
}
96
- $ itemId = $ item ['cart_item_id ' ];
114
+ $ itemId = ( int ) $ item ['cart_item_id ' ];
97
115
98
116
if (!isset ($ item ['quantity ' ])) {
99
117
throw new GraphQlInputException (__ ('Required parameter "quantity" for "cart_items" is missing. ' ));
100
118
}
101
119
$ qty = (float )$ item ['quantity ' ];
102
120
103
- $ cartItem = $ cart ->getItemById ($ itemId );
104
- if ($ cartItem === false ) {
105
- throw new GraphQlNoSuchEntityException (
106
- __ ('Could not find cart item with id: %1. ' , $ item ['cart_item_id ' ])
107
- );
108
- }
109
-
110
121
if ($ qty <= 0.0 ) {
111
122
$ this ->cartItemRepository ->deleteById ((int )$ cart ->getId (), $ itemId );
112
123
} else {
113
- $ cartItem ->setQty ($ qty );
114
- $ this ->cartItemRepository ->save ($ cartItem );
124
+ $ customizableOptions = $ item ['customizable_options ' ] ?? null ;
125
+
126
+ if ($ customizableOptions === null ) { // Update only item's qty
127
+ $ this ->updateItemQty ($ itemId , $ cart , $ qty );
128
+ } else { // Update customizable options (and QTY if changed)
129
+ $ this ->updateCartItem ->execute ($ cart , $ itemId , $ qty , $ customizableOptions );
130
+ $ this ->quoteRepository ->save ($ cart );
131
+ }
115
132
}
116
133
}
117
134
}
135
+
136
+ /**
137
+ * Updates item qty for the specified cart
138
+ *
139
+ * @param int $itemId
140
+ * @param Quote $cart
141
+ * @param float $qty
142
+ * @throws GraphQlNoSuchEntityException
143
+ * @throws NoSuchEntityException
144
+ * @throws GraphQlNoSuchEntityException
145
+ */
146
+ private function updateItemQty (int $ itemId , Quote $ cart , float $ qty )
147
+ {
148
+ $ cartItem = $ cart ->getItemById ($ itemId );
149
+ if ($ cartItem === false ) {
150
+ throw new GraphQlNoSuchEntityException (
151
+ __ ('Could not find cart item with id: %1. ' , $ itemId )
152
+ );
153
+ }
154
+ $ cartItem ->setQty ($ qty );
155
+ $ this ->cartItemRepository ->save ($ cartItem );
156
+ }
118
157
}
0 commit comments