Skip to content

Commit 8dc3ab8

Browse files
author
Prabhu Ram
committed
MC-22213: Implementation - Merge cart
- Merged community PR magento/graphql-ce#950
1 parent 3cc95fb commit 8dc3ab8

File tree

7 files changed

+97
-277
lines changed

7 files changed

+97
-277
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/GetCart.php

Lines changed: 0 additions & 95 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Framework\Exception\NoSuchEntityException;
1111
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1212
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1315
use Magento\Quote\Model\Quote;
1416

1517
/**
@@ -18,17 +20,25 @@
1820
class GetCartForUser
1921
{
2022
/**
21-
* @var GetCart
23+
* @var MaskedQuoteIdToQuoteIdInterface
2224
*/
23-
private $getCart;
25+
private $maskedQuoteIdToQuoteId;
2426

2527
/**
26-
* @param GetCart $getCart
28+
* @var CartRepositoryInterface
29+
*/
30+
private $cartRepository;
31+
32+
/**
33+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
34+
* @param CartRepositoryInterface $cartRepository
2735
*/
2836
public function __construct(
29-
GetCart $getCart
37+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
38+
CartRepositoryInterface $cartRepository
3039
) {
31-
$this->getCart = $getCart;
40+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
41+
$this->cartRepository = $cartRepository;
3242
}
3343

3444
/**
@@ -44,14 +54,38 @@ public function __construct(
4454
*/
4555
public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
4656
{
47-
$cart = $this->getCart->execute($cartHash, $customerId, $storeId);
57+
try {
58+
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
59+
} catch (NoSuchEntityException $exception) {
60+
throw new GraphQlNoSuchEntityException(
61+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
62+
);
63+
}
64+
65+
try {
66+
/** @var Quote $cart */
67+
$cart = $this->cartRepository->get($cartId);
68+
} catch (NoSuchEntityException $e) {
69+
throw new GraphQlNoSuchEntityException(
70+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
71+
);
72+
}
4873

4974
if (false === (bool)$cart->getIsActive()) {
5075
throw new GraphQlNoSuchEntityException(
5176
__('Current user does not have an active cart.')
5277
);
5378
}
5479

80+
if ((int)$cart->getStoreId() !== $storeId) {
81+
throw new GraphQlNoSuchEntityException(
82+
__(
83+
'Wrong store code specified for cart "%masked_cart_id"',
84+
['masked_cart_id' => $cartHash]
85+
)
86+
);
87+
}
88+
5589
$cartCustomerId = (int)$cart->getCustomerId();
5690

5791
/* Guest cart, allow operations */

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

Lines changed: 0 additions & 91 deletions
This file was deleted.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1515
use Magento\Quote\Api\CartRepositoryInterface;
16+
use Magento\GraphQl\Model\Query\ContextInterface;
17+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1618

1719
/**
1820
* Merge Carts Resolver
@@ -54,6 +56,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5456
throw new GraphQlInputException(__('Required parameter "destination_cart_id" is missing'));
5557
}
5658

59+
/** @var ContextInterface $context */
60+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
61+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
62+
}
63+
5764
$guestMaskedCartId = $args['source_cart_id'];
5865
$customerMaskedCartId = $args['destination_cart_id'];
5966

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type Mutation {
2121
setPaymentMethodAndPlaceOrder(input: SetPaymentMethodAndPlaceOrderInput): PlaceOrderOutput @deprecated(reason: "Should use setPaymentMethodOnCart and placeOrder mutations in single request.") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
2222
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")
2323
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
24-
mergeCarts(input: MergeCartsInput): String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
2524
}
2625

2726
input createEmptyCartInput {
@@ -148,11 +147,6 @@ input SetGuestEmailOnCartInput {
148147
email: String!
149148
}
150149

151-
input MergeCartsInput {
152-
first_cart_id: String!
153-
second_cart_id: String!
154-
}
155-
156150
type CartPrices {
157151
grand_total: Money
158152
subtotal_including_tax: Money

0 commit comments

Comments
 (0)