Skip to content

Commit 3cc95fb

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'pmclain/issue/905' into MC-22181
2 parents f19f727 + 6a61d04 commit 3cc95fb

File tree

6 files changed

+625
-40
lines changed

6 files changed

+625
-40
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
14+
use Magento\Quote\Model\Quote;
15+
16+
/**
17+
* Get cart merge
18+
*/
19+
class GetCart
20+
{
21+
/**
22+
* @var MaskedQuoteIdToQuoteIdInterface
23+
*/
24+
private $maskedQuoteIdToQuoteId;
25+
26+
/**
27+
* @var CartRepositoryInterface
28+
*/
29+
private $cartRepository;
30+
31+
/**
32+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
33+
* @param CartRepositoryInterface $cartRepository
34+
*/
35+
public function __construct(
36+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
37+
CartRepositoryInterface $cartRepository
38+
) {
39+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
40+
$this->cartRepository = $cartRepository;
41+
}
42+
43+
/**
44+
* Get cart for merge
45+
*
46+
* @param string $cartHash
47+
* @param int|null $customerId
48+
* @param int $storeId
49+
* @return Quote
50+
* @throws GraphQlNoSuchEntityException
51+
* @throws NoSuchEntityException
52+
*/
53+
public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
54+
{
55+
try {
56+
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
57+
} catch (NoSuchEntityException $exception) {
58+
throw new GraphQlNoSuchEntityException(
59+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
60+
);
61+
}
62+
63+
try {
64+
/** @var Quote $cart */
65+
$cart = $this->cartRepository->get($cartId);
66+
} catch (NoSuchEntityException $e) {
67+
throw new GraphQlNoSuchEntityException(
68+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
69+
);
70+
}
71+
72+
if ((int)$cart->getStoreId() !== $storeId) {
73+
throw new GraphQlNoSuchEntityException(
74+
__(
75+
'Wrong store code specified for cart "%masked_cart_id"',
76+
['masked_cart_id' => $cartHash]
77+
)
78+
);
79+
}
80+
81+
$cartCustomerId = (int)$cart->getCustomerId();
82+
83+
/* Guest cart, allow operations */
84+
if (0 === $cartCustomerId) {
85+
return $cart;
86+
}
87+
88+
if ($cartCustomerId !== $customerId) {
89+
throw new GraphQlNoSuchEntityException(
90+
__('The current user cannot perform operations on cart "%masked_cart_id"', ['masked_cart_id' => $cartHash])
91+
);
92+
}
93+
return $cart;
94+
}
95+
}

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

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
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;
1513
use Magento\Quote\Model\Quote;
1614

1715
/**
@@ -20,25 +18,17 @@
2018
class GetCartForUser
2119
{
2220
/**
23-
* @var MaskedQuoteIdToQuoteIdInterface
21+
* @var GetCart
2422
*/
25-
private $maskedQuoteIdToQuoteId;
23+
private $getCart;
2624

2725
/**
28-
* @var CartRepositoryInterface
29-
*/
30-
private $cartRepository;
31-
32-
/**
33-
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
34-
* @param CartRepositoryInterface $cartRepository
26+
* @param GetCart $getCart
3527
*/
3628
public function __construct(
37-
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
38-
CartRepositoryInterface $cartRepository
29+
GetCart $getCart
3930
) {
40-
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
41-
$this->cartRepository = $cartRepository;
31+
$this->getCart = $getCart;
4232
}
4333

4434
/**
@@ -54,38 +44,14 @@ public function __construct(
5444
*/
5545
public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
5646
{
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-
}
47+
$cart = $this->getCart->execute($cartHash, $customerId, $storeId);
7348

7449
if (false === (bool)$cart->getIsActive()) {
7550
throw new GraphQlNoSuchEntityException(
7651
__('Current user does not have an active cart.')
7752
);
7853
}
7954

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-
8955
$cartCustomerId = (int)$cart->getCustomerId();
9056

9157
/* Guest cart, allow operations */
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Quote\Model\Quote;
11+
use Magento\Quote\Model\QuoteIdMask;
12+
use Magento\Quote\Model\QuoteIdMaskFactory;
13+
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;
14+
use Magento\Quote\Api\CartRepositoryInterface;
15+
16+
/**
17+
* Merge two carts
18+
*/
19+
class MergeCarts
20+
{
21+
/**
22+
* @var QuoteIdMaskFactory
23+
*/
24+
private $quoteMaskFactory;
25+
26+
/**
27+
* @var QuoteIdMaskResourceModel
28+
*/
29+
private $quoteMaskResource;
30+
31+
/**
32+
* @var CartRepositoryInterface
33+
*/
34+
private $cartRepository;
35+
36+
/**
37+
* @param QuoteIdMaskFactory $quoteMaskFactory
38+
* @param QuoteIdMaskResourceModel $quoteMaskResource
39+
* @param CartRepositoryInterface $cartRepository
40+
*/
41+
public function __construct(
42+
QuoteIdMaskFactory $quoteMaskFactory,
43+
QuoteIdMaskResourceModel $quoteMaskResource,
44+
CartRepositoryInterface $cartRepository
45+
) {
46+
$this->quoteMaskFactory = $quoteMaskFactory;
47+
$this->quoteMaskResource = $quoteMaskResource;
48+
$this->cartRepository = $cartRepository;
49+
}
50+
51+
/**
52+
* Merge two quotes
53+
*
54+
* @param Quote $firstCart
55+
* @param Quote $secondQuote
56+
* @return string
57+
*/
58+
public function execute(Quote $firstCart, Quote $secondQuote): string
59+
{
60+
$firstCart->merge($secondQuote);
61+
$firstCart->setIsActive(true);
62+
63+
$this->updateMaskedId($secondQuote);
64+
$maskedQuoteId = $this->updateMaskedId($firstCart);
65+
66+
$this->cartRepository->save($firstCart);
67+
68+
$secondQuote->setIsActive(false);
69+
$this->cartRepository->save($secondQuote);
70+
71+
return $maskedQuoteId;
72+
}
73+
74+
/**
75+
* Update quote masked id
76+
*
77+
* @param Quote $quote
78+
* @return string
79+
*/
80+
private function updateMaskedId(Quote $quote): string
81+
{
82+
/** @var QuoteIdMask $quoteIdMask */
83+
$quoteIdMask = $this->quoteMaskFactory->create();
84+
$this->quoteMaskResource->load($quoteIdMask, $quote->getId(), 'quote_id');
85+
$quoteIdMask->unsetData('masked_id');
86+
$this->quoteMaskResource->save($quoteIdMask);
87+
$maskedId = $quoteIdMask->getMaskedId();
88+
89+
return $maskedId;
90+
}
91+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ 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")
2425
}
2526

2627
input createEmptyCartInput {
@@ -147,6 +148,11 @@ input SetGuestEmailOnCartInput {
147148
email: String!
148149
}
149150

151+
input MergeCartsInput {
152+
first_cart_id: String!
153+
second_cart_id: String!
154+
}
155+
150156
type CartPrices {
151157
grand_total: Money
152158
subtotal_including_tax: Money

0 commit comments

Comments
 (0)