11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
12
use Magento \Framework \DataObject ;
13
13
use Magento \Framework \DataObjectFactory ;
14
+ use Magento \Framework \Exception \NoSuchEntityException ;
14
15
use Magento \Framework \GraphQl \Config \Element \Field ;
15
16
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
16
17
use Magento \Framework \GraphQl \Query \Resolver \Value ;
19
20
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
20
21
use Magento \Framework \Message \AbstractMessage ;
21
22
use Magento \Framework \Stdlib \ArrayManager ;
23
+ use Magento \Quote \Api \CartRepositoryInterface ;
22
24
use Magento \Quote \Api \Data \CartInterface ;
23
25
use Magento \Quote \Api \GuestCartRepositoryInterface ;
26
+ use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
24
27
use Magento \Quote \Model \Quote ;
25
- use Magento \Quote \Model \QuoteRepository ;
26
28
use Magento \QuoteGraphQl \Model \Hydrator \CartHydrator ;
27
29
28
30
/**
29
31
* {@inheritdoc}
30
32
*/
31
33
class AddSimpleProductsToCart implements ResolverInterface
32
34
{
35
+ /**
36
+ * @var CartRepositoryInterface
37
+ */
38
+ private $ cartRepository ;
39
+
40
+ /**
41
+ * @var MaskedQuoteIdToQuoteIdInterface
42
+ */
43
+ private $ maskedQuoteIdToQuoteId ;
44
+
33
45
/**
34
46
* @var DataObjectFactory
35
47
*/
@@ -40,11 +52,6 @@ class AddSimpleProductsToCart implements ResolverInterface
40
52
*/
41
53
private $ guestCartRepository ;
42
54
43
- /**
44
- * @var QuoteRepository
45
- */
46
- private $ quoteRepository ;
47
-
48
55
/**
49
56
* @var ProductRepositoryInterface
50
57
*/
@@ -74,8 +81,9 @@ class AddSimpleProductsToCart implements ResolverInterface
74
81
* @param DataObjectFactory $dataObjectFactory
75
82
* @param CartHydrator $cartHydrator
76
83
* @param ArrayManager $arrayManager
84
+ * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
85
+ * @param CartRepositoryInterface $cartRepository
77
86
* @param GuestCartRepositoryInterface $guestCartRepository
78
- * @param QuoteRepository $quoteRepository
79
87
* @param ProductRepositoryInterface $productRepository
80
88
* @param ValueFactory $valueFactory
81
89
* @param UserContextInterface $userContext
@@ -84,8 +92,9 @@ public function __construct(
84
92
DataObjectFactory $ dataObjectFactory ,
85
93
CartHydrator $ cartHydrator ,
86
94
ArrayManager $ arrayManager ,
95
+ MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
96
+ CartRepositoryInterface $ cartRepository ,
87
97
GuestCartRepositoryInterface $ guestCartRepository ,
88
- QuoteRepository $ quoteRepository ,
89
98
ProductRepositoryInterface $ productRepository ,
90
99
ValueFactory $ valueFactory ,
91
100
UserContextInterface $ userContext
@@ -95,20 +104,21 @@ public function __construct(
95
104
$ this ->arrayManager = $ arrayManager ;
96
105
$ this ->productRepository = $ productRepository ;
97
106
$ this ->cartHydrator = $ cartHydrator ;
98
- $ this ->quoteRepository = $ quoteRepository ;
99
107
$ this ->guestCartRepository = $ guestCartRepository ;
100
108
$ this ->dataObjectFactory = $ dataObjectFactory ;
109
+ $ this ->cartRepository = $ cartRepository ;
110
+ $ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
101
111
}
102
112
103
113
/**
104
114
* {@inheritDoc}
105
115
*/
106
116
public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null ) : Value
107
117
{
108
- $ cartId = $ this ->arrayManager ->get ('input/cart_id ' , $ args );
118
+ $ cartHash = $ this ->arrayManager ->get ('input/cart_id ' , $ args );
109
119
$ cartItems = $ this ->arrayManager ->get ('input/cartItems ' , $ args );
110
120
111
- if (!isset ($ cartId )) {
121
+ if (!isset ($ cartHash )) {
112
122
throw new GraphQlInputException (
113
123
__ ('Missing key %1 in cart data ' , ['cart_id ' ])
114
124
);
@@ -120,8 +130,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
120
130
);
121
131
}
122
132
123
- /** @var CartInterface|Quote $cart */
124
- $ cart = $ this ->guestCartRepository ->get ($ cartId );
133
+ $ cart = $ this ->getCart ((string ) $ cartHash );
125
134
126
135
foreach ($ cartItems as $ cartItem ) {
127
136
$ sku = $ this ->arrayManager ->get ('details/sku ' , $ cartItem );
@@ -134,7 +143,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
134
143
}
135
144
}
136
145
137
- $ this ->quoteRepository ->save ($ cart );
146
+ $ this ->cartRepository ->save ($ cart );
138
147
139
148
$ result = function () use ($ cart ) {
140
149
return [
@@ -184,4 +193,21 @@ private function getCartErrors($cart): string
184
193
185
194
return implode (PHP_EOL , $ errorMessages );
186
195
}
196
+
197
+ /**
198
+ * @param string $cartHash
199
+ * @return CartInterface|Quote
200
+ * @throws NoSuchEntityException
201
+ */
202
+ private function getCart (string $ cartHash ): CartInterface
203
+ {
204
+ $ customerId = $ this ->userContext ->getUserId ();
205
+
206
+ if (!$ customerId ) {
207
+ return $ this ->guestCartRepository ->get ($ cartHash );
208
+ }
209
+
210
+ $ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ((string ) $ cartHash );
211
+ return $ this ->cartRepository ->get ($ cartId );
212
+ }
187
213
}
0 commit comments