77
88namespace Magento \QuoteGraphQl \Model \Resolver ;
99
10+ use Magento \Framework \App \ObjectManager ;
11+ use Magento \Framework \Exception \CouldNotSaveException ;
1012use Magento \Framework \GraphQl \Config \Element \Field ;
13+ use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
1114use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
1216use Magento \Framework \GraphQl \Query \ResolverInterface ;
1317use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14- use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
15- use Magento \Quote \Api \CartRepositoryInterface ;
1618use Magento \GraphQl \Model \Query \ContextInterface ;
17- use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
19+ use Magento \Quote \Api \CartRepositoryInterface ;
20+ use Magento \Quote \Model \Cart \CustomerCartResolver ;
21+ use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
22+ use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
1823
1924/**
2025 * Merge Carts Resolver
26+ *
27+ * @SuppressWarnings(PHPMD.LongVariable)
2128 */
2229class MergeCarts implements ResolverInterface
2330{
@@ -31,44 +38,95 @@ class MergeCarts implements ResolverInterface
3138 */
3239 private $ cartRepository ;
3340
41+ /**
42+ * @var CustomerCartResolver
43+ */
44+ private $ customerCartResolver ;
45+
46+ /**
47+ * @var QuoteIdToMaskedQuoteIdInterface
48+ */
49+ private $ quoteIdToMaskedQuoteId ;
50+
3451 /**
3552 * @param GetCartForUser $getCartForUser
3653 * @param CartRepositoryInterface $cartRepository
54+ * @param CustomerCartResolver|null $customerCartResolver
55+ * @param QuoteIdToMaskedQuoteIdInterface|null $quoteIdToMaskedQuoteId
3756 */
3857 public function __construct (
3958 GetCartForUser $ getCartForUser ,
40- CartRepositoryInterface $ cartRepository
59+ CartRepositoryInterface $ cartRepository ,
60+ CustomerCartResolver $ customerCartResolver = null ,
61+ QuoteIdToMaskedQuoteIdInterface $ quoteIdToMaskedQuoteId = null
4162 ) {
4263 $ this ->getCartForUser = $ getCartForUser ;
4364 $ this ->cartRepository = $ cartRepository ;
65+ $ this ->customerCartResolver = $ customerCartResolver
66+ ?: ObjectManager::getInstance ()->get (CustomerCartResolver::class);
67+ $ this ->quoteIdToMaskedQuoteId = $ quoteIdToMaskedQuoteId
68+ ?: ObjectManager::getInstance ()->get (QuoteIdToMaskedQuoteIdInterface::class);
4469 }
4570
4671 /**
4772 * @inheritdoc
4873 */
49- public function resolve (Field $ field , $ context , ResolveInfo $ info , array $ value = null , array $ args = null )
50- {
74+ public function resolve (
75+ Field $ field ,
76+ $ context ,
77+ ResolveInfo $ info ,
78+ array $ value = null ,
79+ array $ args = null
80+ ) {
5181 if (empty ($ args ['source_cart_id ' ])) {
52- throw new GraphQlInputException (__ ('Required parameter "source_cart_id" is missing ' ));
53- }
54-
55- if (empty ($ args ['destination_cart_id ' ])) {
56- throw new GraphQlInputException (__ ('Required parameter "destination_cart_id" is missing ' ));
82+ throw new GraphQlInputException (__ (
83+ 'Required parameter "source_cart_id" is missing '
84+ ));
5785 }
5886
5987 /** @var ContextInterface $context */
6088 if (false === $ context ->getExtensionAttributes ()->getIsCustomer ()) {
61- throw new GraphQlAuthorizationException (__ ('The current customer isn \'t authorized. ' ));
89+ throw new GraphQlAuthorizationException (__ (
90+ 'The current customer isn \'t authorized. '
91+ ));
92+ }
93+ $ currentUserId = $ context ->getUserId ();
94+
95+ if (!isset ($ args ['destination_cart_id ' ])) {
96+ try {
97+ $ cart = $ this ->customerCartResolver ->resolve ($ currentUserId );
98+ } catch (CouldNotSaveException $ exception ) {
99+ throw new GraphQlNoSuchEntityException (
100+ __ ('Could not create empty cart for customer ' ),
101+ $ exception
102+ );
103+ }
104+ $ customerMaskedCartId = $ this ->quoteIdToMaskedQuoteId ->execute (
105+ (int ) $ cart ->getId ()
106+ );
107+ } else {
108+ if (empty ($ args ['destination_cart_id ' ])) {
109+ throw new GraphQlInputException (__ (
110+ 'The parameter "destination_cart_id" cannot be empty '
111+ ));
112+ }
62113 }
63114
64115 $ guestMaskedCartId = $ args ['source_cart_id ' ];
65- $ customerMaskedCartId = $ args ['destination_cart_id ' ];
116+ $ customerMaskedCartId = $ customerMaskedCartId ?? $ args ['destination_cart_id ' ];
66117
67- $ currentUserId = $ context ->getUserId ();
68118 $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
69119 // passing customerId as null enforces source cart should always be a guestcart
70- $ guestCart = $ this ->getCartForUser ->execute ($ guestMaskedCartId , null , $ storeId );
71- $ customerCart = $ this ->getCartForUser ->execute ($ customerMaskedCartId , $ currentUserId , $ storeId );
120+ $ guestCart = $ this ->getCartForUser ->execute (
121+ $ guestMaskedCartId ,
122+ null ,
123+ $ storeId
124+ );
125+ $ customerCart = $ this ->getCartForUser ->execute (
126+ $ customerMaskedCartId ,
127+ $ currentUserId ,
128+ $ storeId
129+ );
72130 $ customerCart ->merge ($ guestCart );
73131 $ guestCart ->setIsActive (false );
74132 $ this ->cartRepository ->save ($ customerCart );
0 commit comments