1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2019 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
8
8
namespace Magento \QuoteGraphQl \Model \Resolver ;
9
9
10
- use Magento \CatalogInventory \Api \StockRegistryInterface ;
11
- use Magento \Framework \App \ObjectManager ;
12
10
use Magento \Framework \Exception \CouldNotSaveException ;
13
- use Magento \Framework \Exception \NoSuchEntityException ;
14
11
use Magento \Framework \GraphQl \Config \Element \Field ;
15
12
use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
16
13
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
17
14
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
18
15
use Magento \Framework \GraphQl \Query \ResolverInterface ;
19
16
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
20
17
use Magento \GraphQl \Model \Query \ContextInterface ;
21
- use Magento \Quote \Api \CartItemRepositoryInterface ;
22
18
use Magento \Quote \Api \CartRepositoryInterface ;
23
- use Magento \Quote \Api \Data \CartInterface ;
24
- use Magento \Quote \Api \Data \CartItemInterface ;
25
19
use Magento \Quote \Model \Cart \CustomerCartResolver ;
20
+ use Magento \Quote \Model \Quote ;
26
21
use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
27
22
use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
28
23
use Magento \QuoteGraphQl \Model \Cart \MergeCarts \CartQuantityValidatorInterface ;
29
24
30
- /**
31
- * Merge Carts Resolver
32
- *
33
- * @SuppressWarnings(PHPMD.LongVariable)
34
- */
35
25
class MergeCarts implements ResolverInterface
36
26
{
37
27
/**
38
- * @var GetCartForUser
39
- */
40
- private $ getCartForUser ;
41
-
42
- /**
43
- * @var CartRepositoryInterface
44
- */
45
- private $ cartRepository ;
46
-
47
- /**
48
- * @var CustomerCartResolver
49
- */
50
- private $ customerCartResolver ;
51
-
52
- /**
53
- * @var QuoteIdToMaskedQuoteIdInterface
54
- */
55
- private $ quoteIdToMaskedQuoteId ;
56
-
57
- /**
58
- * @var CartItemRepositoryInterface
59
- */
60
- private $ cartItemRepository ;
61
-
62
- /**
63
- * @var StockRegistryInterface
64
- */
65
- private $ stockRegistry ;
66
-
67
- /**
68
- * @var CartQuantityValidatorInterface
28
+ * @var array
69
29
*/
70
- private $ cartQuantityValidator ;
30
+ private array $ fields ;
71
31
72
32
/**
33
+ * MergeCarts Constructor
34
+ *
73
35
* @param GetCartForUser $getCartForUser
74
36
* @param CartRepositoryInterface $cartRepository
75
- * @param CustomerCartResolver|null $customerCartResolver
76
- * @param QuoteIdToMaskedQuoteIdInterface|null $quoteIdToMaskedQuoteId
77
- * @param CartItemRepositoryInterface|null $cartItemRepository
78
- * @param StockRegistryInterface|null $stockRegistry
37
+ * @param CustomerCartResolver $customerCartResolver
38
+ * @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
39
+ * @param CartQuantityValidatorInterface $cartQuantityValidator
40
+ * @param array $fields
79
41
*/
80
42
public function __construct (
81
- GetCartForUser $ getCartForUser ,
82
- CartRepositoryInterface $ cartRepository ,
83
- ?CustomerCartResolver $ customerCartResolver = null ,
84
- ?QuoteIdToMaskedQuoteIdInterface $ quoteIdToMaskedQuoteId = null ,
85
- ?CartItemRepositoryInterface $ cartItemRepository = null ,
86
- ?StockRegistryInterface $ stockRegistry = null ,
87
- ?CartQuantityValidatorInterface $ cartQuantityValidator = null
43
+ private readonly GetCartForUser $ getCartForUser ,
44
+ private readonly CartRepositoryInterface $ cartRepository ,
45
+ private readonly CustomerCartResolver $ customerCartResolver ,
46
+ private readonly QuoteIdToMaskedQuoteIdInterface $ quoteIdToMaskedQuoteId ,
47
+ private readonly CartQuantityValidatorInterface $ cartQuantityValidator ,
48
+ array $ fields
88
49
) {
89
- $ this ->getCartForUser = $ getCartForUser ;
90
- $ this ->cartRepository = $ cartRepository ;
91
- $ this ->customerCartResolver = $ customerCartResolver
92
- ?: ObjectManager::getInstance ()->get (CustomerCartResolver::class);
93
- $ this ->quoteIdToMaskedQuoteId = $ quoteIdToMaskedQuoteId
94
- ?: ObjectManager::getInstance ()->get (QuoteIdToMaskedQuoteIdInterface::class);
95
- $ this ->cartItemRepository = $ cartItemRepository
96
- ?: ObjectManager::getInstance ()->get (CartItemRepositoryInterface::class);
97
- $ this ->stockRegistry = $ stockRegistry
98
- ?: ObjectManager::getInstance ()->get (StockRegistryInterface::class);
99
- $ this ->cartQuantityValidator = $ cartQuantityValidator
100
- ?: ObjectManager::getInstance ()->get (CartQuantityValidatorInterface::class);
50
+ $ this ->fields = $ fields ;
101
51
}
102
52
103
53
/**
@@ -109,69 +59,75 @@ public function resolve(
109
59
ResolveInfo $ info ,
110
60
?array $ value = null ,
111
61
?array $ args = null
112
- ) {
62
+ ): array {
113
63
if (empty ($ args ['source_cart_id ' ])) {
114
- throw new GraphQlInputException (__ (
115
- 'Required parameter "source_cart_id" is missing '
116
- ));
64
+ throw new GraphQlInputException (__ ('Required parameter "source_cart_id" is missing ' ));
65
+ }
66
+
67
+ if (isset ($ args ['destination_cart_id ' ]) && empty ($ args ['destination_cart_id ' ])) {
68
+ throw new GraphQlInputException (__ ('The parameter "destination_cart_id" cannot be empty ' ));
117
69
}
118
70
119
71
/** @var ContextInterface $context */
120
- if (false === $ context ->getExtensionAttributes ()->getIsCustomer ()) {
121
- throw new GraphQlAuthorizationException (__ (
122
- 'The current customer isn \'t authorized. '
123
- ));
72
+ if (!$ context ->getExtensionAttributes ()->getIsCustomer ()) {
73
+ throw new GraphQlAuthorizationException (__ ('The current customer isn \'t authorized. ' ));
124
74
}
75
+
125
76
$ currentUserId = $ context ->getUserId ();
77
+ $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
78
+ $ guestMaskedCartId = $ args ['source_cart_id ' ];
79
+
80
+ // Resolve destination cart ID
81
+ $ customerMaskedCartId = $ args ['destination_cart_id ' ] ?? null ;
126
82
127
- if (!isset ( $ args [ ' destination_cart_id ' ]) ) {
83
+ if (!$ customerMaskedCartId ) {
128
84
try {
129
85
$ cart = $ this ->customerCartResolver ->resolve ($ currentUserId );
86
+ $ customerMaskedCartId = $ this ->quoteIdToMaskedQuoteId ->execute ((int ) $ cart ->getId ());
130
87
} catch (CouldNotSaveException $ exception ) {
131
88
throw new GraphQlNoSuchEntityException (
132
89
__ ('Could not create empty cart for customer ' ),
133
90
$ exception
134
91
);
135
92
}
136
- $ customerMaskedCartId = $ this ->quoteIdToMaskedQuoteId ->execute (
137
- (int ) $ cart ->getId ()
138
- );
139
- } else {
140
- if (empty ($ args ['destination_cart_id ' ])) {
141
- throw new GraphQlInputException (__ (
142
- 'The parameter "destination_cart_id" cannot be empty '
143
- ));
144
- }
145
93
}
146
94
147
- $ guestMaskedCartId = $ args ['source_cart_id ' ];
148
- $ customerMaskedCartId = $ customerMaskedCartId ?? $ args ['destination_cart_id ' ];
95
+ // Fetch guest and customer carts
96
+ $ customerCart = $ this ->getCartForUser ->execute ($ customerMaskedCartId , $ currentUserId , $ storeId );
97
+ $ guestCart = $ this ->getCartForUser ->execute ($ guestMaskedCartId , null , $ storeId );
149
98
150
- $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
151
- // passing customerId as null enforces source cart should always be a guestcart
152
- $ guestCart = $ this ->getCartForUser ->execute (
153
- $ guestMaskedCartId ,
154
- null ,
155
- $ storeId
156
- );
157
- $ customerCart = $ this ->getCartForUser ->execute (
158
- $ customerMaskedCartId ,
159
- $ currentUserId ,
160
- $ storeId
161
- );
99
+ // Validate cart quantities before merging
162
100
if ($ this ->cartQuantityValidator ->validateFinalCartQuantities ($ customerCart , $ guestCart )) {
163
- $ guestCart = $ this ->getCartForUser ->execute (
164
- $ guestMaskedCartId ,
165
- null ,
166
- $ storeId
167
- );
101
+ $ guestCart = $ this ->getCartForUser ->execute ($ guestMaskedCartId , null , $ storeId );
168
102
}
103
+
104
+ // Merge carts and save
169
105
$ customerCart ->merge ($ guestCart );
170
106
$ guestCart ->setIsActive (false );
107
+ // Check and update gift options from guest cart to customer cart
108
+ $ customerCart = $ this ->updateGiftOptions ($ guestCart , $ customerCart );
109
+
171
110
$ this ->cartRepository ->save ($ customerCart );
172
111
$ this ->cartRepository ->save ($ guestCart );
173
- return [
174
- 'model ' => $ customerCart ,
175
- ];
112
+
113
+ return ['model ' => $ customerCart ];
114
+ }
115
+
116
+ /**
117
+ * Check and update gift options in customer cart from guest cart
118
+ *
119
+ * @param Quote $guestCart
120
+ * @param Quote $customerCart
121
+ * @return Quote
122
+ */
123
+ private function updateGiftOptions (Quote $ guestCart , Quote $ customerCart ): Quote
124
+ {
125
+ foreach ($ this ->fields as $ field ) {
126
+ if (!empty ($ guestCart ->getData ($ field )) && empty ($ customerCart ->getData ($ field ))) {
127
+ $ customerCart ->setData ($ field , $ guestCart ->getData ($ field ));
128
+ }
129
+ }
130
+
131
+ return $ customerCart ;
176
132
}
177
133
}
0 commit comments