7
7
8
8
namespace Magento \QuoteGraphQl \Model \Resolver ;
9
9
10
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \Exception \CouldNotSaveException ;
13
+ use Magento \Framework \Exception \NoSuchEntityException ;
12
14
use Magento \Framework \GraphQl \Config \Element \Field ;
13
15
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
14
16
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
17
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
16
18
use Magento \Framework \GraphQl \Query \ResolverInterface ;
17
19
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
18
20
use Magento \GraphQl \Model \Query \ContextInterface ;
21
+ use Magento \Quote \Api \CartItemRepositoryInterface ;
19
22
use Magento \Quote \Api \CartRepositoryInterface ;
23
+ use Magento \Quote \Api \Data \CartInterface ;
24
+ use Magento \Quote \Api \Data \CartItemInterface ;
20
25
use Magento \Quote \Model \Cart \CustomerCartResolver ;
21
26
use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
22
27
use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
@@ -48,24 +53,42 @@ class MergeCarts implements ResolverInterface
48
53
*/
49
54
private $ quoteIdToMaskedQuoteId ;
50
55
56
+ /**
57
+ * @var CartItemRepositoryInterface
58
+ */
59
+ private $ cartItemRepository ;
60
+
61
+ /**
62
+ * @var StockRegistryInterface
63
+ */
64
+ private $ stockRegistry ;
65
+
51
66
/**
52
67
* @param GetCartForUser $getCartForUser
53
68
* @param CartRepositoryInterface $cartRepository
54
69
* @param CustomerCartResolver|null $customerCartResolver
55
70
* @param QuoteIdToMaskedQuoteIdInterface|null $quoteIdToMaskedQuoteId
71
+ * @param CartItemRepositoryInterface|null $cartItemRepository
72
+ * @param StockRegistryInterface|null $stockRegistry
56
73
*/
57
74
public function __construct (
58
75
GetCartForUser $ getCartForUser ,
59
76
CartRepositoryInterface $ cartRepository ,
60
77
CustomerCartResolver $ customerCartResolver = null ,
61
- QuoteIdToMaskedQuoteIdInterface $ quoteIdToMaskedQuoteId = null
78
+ QuoteIdToMaskedQuoteIdInterface $ quoteIdToMaskedQuoteId = null ,
79
+ CartItemRepositoryInterface $ cartItemRepository = null ,
80
+ StockRegistryInterface $ stockRegistry = null
62
81
) {
63
82
$ this ->getCartForUser = $ getCartForUser ;
64
83
$ this ->cartRepository = $ cartRepository ;
65
84
$ this ->customerCartResolver = $ customerCartResolver
66
85
?: ObjectManager::getInstance ()->get (CustomerCartResolver::class);
67
86
$ this ->quoteIdToMaskedQuoteId = $ quoteIdToMaskedQuoteId
68
87
?: ObjectManager::getInstance ()->get (QuoteIdToMaskedQuoteIdInterface::class);
88
+ $ this ->cartItemRepository = $ cartItemRepository
89
+ ?: ObjectManager::getInstance ()->get (CartItemRepositoryInterface::class);
90
+ $ this ->stockRegistry = $ stockRegistry
91
+ ?: ObjectManager::getInstance ()->get (StockRegistryInterface::class);
69
92
}
70
93
71
94
/**
@@ -127,6 +150,13 @@ public function resolve(
127
150
$ currentUserId ,
128
151
$ storeId
129
152
);
153
+ if ($ this ->validateFinalCartQuantities ($ customerCart , $ guestCart )) {
154
+ $ guestCart = $ this ->getCartForUser ->execute (
155
+ $ guestMaskedCartId ,
156
+ null ,
157
+ $ storeId
158
+ );
159
+ }
130
160
$ customerCart ->merge ($ guestCart );
131
161
$ guestCart ->setIsActive (false );
132
162
$ this ->cartRepository ->save ($ customerCart );
@@ -135,4 +165,39 @@ public function resolve(
135
165
'model ' => $ customerCart ,
136
166
];
137
167
}
168
+
169
+ /**
170
+ * Validate combined cart quantities to make sure they are within available stock
171
+ *
172
+ * @param CartInterface $customerCart
173
+ * @param CartInterface $guestCart
174
+ * @return bool
175
+ */
176
+ private function validateFinalCartQuantities (CartInterface $ customerCart , CartInterface $ guestCart )
177
+ {
178
+ $ modified = false ;
179
+ /** @var CartItemInterface $guestCartItem */
180
+ foreach ($ guestCart ->getAllVisibleItems () as $ guestCartItem ) {
181
+ foreach ($ customerCart ->getAllItems () as $ customerCartItem ) {
182
+ if ($ customerCartItem ->compare ($ guestCartItem )) {
183
+ $ product = $ customerCartItem ->getProduct ();
184
+ $ stockCurrentQty = $ this ->stockRegistry ->getStockStatus (
185
+ $ product ->getId (),
186
+ $ product ->getStore ()->getWebsiteId ()
187
+ )->getQty ();
188
+ if ($ stockCurrentQty < $ guestCartItem ->getQty () + $ customerCartItem ->getQty ()) {
189
+ try {
190
+ $ this ->cartItemRepository ->deleteById ($ guestCart ->getId (), $ guestCartItem ->getItemId ());
191
+ $ modified = true ;
192
+ } catch (NoSuchEntityException $ e ) {
193
+ continue ;
194
+ } catch (CouldNotSaveException $ e ) {
195
+ continue ;
196
+ }
197
+ }
198
+ }
199
+ }
200
+ }
201
+ return $ modified ;
202
+ }
138
203
}
0 commit comments