Skip to content

Commit 3009cdc

Browse files
andrewbessAndrii Beziazychnyi
authored andcommitted
M2CE-30469: [GRAPHQL] Made the attribute destination_cart_id for mergeCarts mutation not required.
1 parent 39244bf commit 3009cdc

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/MergeCarts.php

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

10+
use Magento\Framework\Exception\CouldNotSaveException;
1011
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1113
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1215
use Magento\Framework\GraphQl\Query\ResolverInterface;
1316
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14-
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
15-
use Magento\Quote\Api\CartRepositoryInterface;
1617
use Magento\GraphQl\Model\Query\ContextInterface;
17-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
18+
use Magento\Quote\Api\CartRepositoryInterface;
19+
use Magento\Quote\Model\Cart\CustomerCartResolver;
20+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
21+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1822

1923
/**
2024
* Merge Carts Resolver
25+
*
26+
* @SuppressWarnings(PHPMD.LongVariable)
2127
*/
2228
class MergeCarts implements ResolverInterface
2329
{
@@ -31,44 +37,87 @@ class MergeCarts implements ResolverInterface
3137
*/
3238
private $cartRepository;
3339

40+
/**
41+
* @var CustomerCartResolver
42+
*/
43+
private $customerCartResolver;
44+
45+
/**
46+
* @var QuoteIdToMaskedQuoteIdInterface
47+
*/
48+
private $quoteIdToMaskedQuoteId;
49+
3450
/**
3551
* @param GetCartForUser $getCartForUser
3652
* @param CartRepositoryInterface $cartRepository
53+
* @param CustomerCartResolver $customerCartResolver
54+
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
3755
*/
3856
public function __construct(
3957
GetCartForUser $getCartForUser,
40-
CartRepositoryInterface $cartRepository
58+
CartRepositoryInterface $cartRepository,
59+
CustomerCartResolver $customerCartResolver,
60+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
4161
) {
4262
$this->getCartForUser = $getCartForUser;
4363
$this->cartRepository = $cartRepository;
64+
$this->customerCartResolver = $customerCartResolver;
65+
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
4466
}
4567

4668
/**
4769
* @inheritdoc
4870
*/
49-
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
50-
{
71+
public function resolve(
72+
Field $field,
73+
$context,
74+
ResolveInfo $info,
75+
array $value = null,
76+
array $args = null
77+
) {
5178
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'));
79+
throw new GraphQlInputException(__(
80+
'Required parameter "source_cart_id" is missing'
81+
));
5782
}
5883

5984
/** @var ContextInterface $context */
6085
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
61-
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
86+
throw new GraphQlAuthorizationException(__(
87+
'The current customer isn\'t authorized.'
88+
));
89+
}
90+
$currentUserId = $context->getUserId();
91+
92+
if (empty($args['destination_cart_id'])) {
93+
try {
94+
$cart = $this->customerCartResolver->resolve($currentUserId);
95+
} catch (CouldNotSaveException $exception) {
96+
throw new GraphQlNoSuchEntityException(
97+
__('Could not create empty cart for customer'),
98+
$exception
99+
);
100+
}
101+
$customerMaskedCartId = $this->quoteIdToMaskedQuoteId->execute(
102+
(int) $cart->getId()
103+
);
62104
}
63105

64106
$guestMaskedCartId = $args['source_cart_id'];
65-
$customerMaskedCartId = $args['destination_cart_id'];
107+
$customerMaskedCartId = $customerMaskedCartId ?? $args['destination_cart_id'];
66108

67-
$currentUserId = $context->getUserId();
68109
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
69110
// 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);
111+
$guestCart = $this->getCartForUser->execute(
112+
$guestMaskedCartId,
113+
null,
114+
$storeId
115+
);
116+
$customerCart = $this->getCartForUser->execute(
117+
$customerMaskedCartId,
118+
$currentUserId,
119+
$storeId
120+
);
72121
$customerCart->merge($guestCart);
73122
$guestCart->setIsActive(false);
74123
$this->cartRepository->save($customerCart);

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Mutation {
2020
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
2121
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
2222
setPaymentMethodAndPlaceOrder(input: SetPaymentMethodAndPlaceOrderInput): PlaceOrderOutput @deprecated(reason: "Should use setPaymentMethodOnCart and placeOrder mutations in single request.") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
23-
mergeCarts(source_cart_id: String!, destination_cart_id: String!): Cart! @doc(description:"Merges the source cart into the destination cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
23+
mergeCarts(source_cart_id: String!, destination_cart_id: String): Cart! @doc(description:"Merges the source cart into the destination cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
2424
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
2525
addProductsToCart(cartId: String!, cartItems: [CartItemInput!]!): AddProductsToCartOutput @doc(description:"Add any type of product to the cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddProductsToCart")
2626
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/MergeCartsTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ public function testMergeCartsWithEmptySourceCartId()
192192
*/
193193
public function testMergeCartsWithEmptyDestinationCartId()
194194
{
195-
$this->expectException(\Exception::class);
196-
$this->expectExceptionMessage('Required parameter "destination_cart_id" is missing');
197-
198195
$guestQuote = $this->quoteFactory->create();
199196
$this->quoteResource->load(
200197
$guestQuote,

0 commit comments

Comments
 (0)