Skip to content

Commit 827d869

Browse files
committed
Merge remote-tracking branch 'origin/MC-37484-cart-query' into HB-PR-delivery-Dec
# Conflicts: # app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveItemFromCart.php
2 parents e0c795f + eff966c commit 827d869

13 files changed

+225
-16
lines changed

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

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1214
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1315
use Magento\Quote\Api\CartRepositoryInterface;
1416
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1517
use Magento\Quote\Model\Quote;
18+
use Magento\Store\Api\StoreRepositoryInterface;
1619

1720
/**
1821
* Get cart
@@ -29,16 +32,24 @@ class GetCartForUser
2932
*/
3033
private $cartRepository;
3134

35+
/**
36+
* @var StoreRepositoryInterface
37+
*/
38+
private $storeRepository;
39+
3240
/**
3341
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
3442
* @param CartRepositoryInterface $cartRepository
43+
* @param StoreRepositoryInterface $storeRepository
3544
*/
3645
public function __construct(
3746
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
38-
CartRepositoryInterface $cartRepository
47+
CartRepositoryInterface $cartRepository,
48+
StoreRepositoryInterface $storeRepository = null
3949
) {
4050
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
4151
$this->cartRepository = $cartRepository;
52+
$this->storeRepository = $storeRepository ?: ObjectManager::getInstance()->get(StoreRepositoryInterface::class);
4253
}
4354

4455
/**
@@ -49,6 +60,7 @@ public function __construct(
4960
* @param int $storeId
5061
* @return Quote
5162
* @throws GraphQlAuthorizationException
63+
* @throws GraphQlInputException
5264
* @throws GraphQlNoSuchEntityException
5365
* @throws NoSuchEntityException
5466
*/
@@ -75,14 +87,7 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
7587
throw new GraphQlNoSuchEntityException(__('The cart isn\'t active.'));
7688
}
7789

78-
if ((int)$cart->getStoreId() !== $storeId) {
79-
throw new GraphQlNoSuchEntityException(
80-
__(
81-
'Wrong store code specified for cart "%masked_cart_id"',
82-
['masked_cart_id' => $cartHash]
83-
)
84-
);
85-
}
90+
$this->updateCartCurrency($cart, $storeId);
8691

8792
$cartCustomerId = (int)$cart->getCustomerId();
8893

@@ -101,4 +106,34 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
101106
}
102107
return $cart;
103108
}
109+
110+
/**
111+
* Sets cart currency based on specified store.
112+
*
113+
* @param Quote $cart
114+
* @param int $storeId
115+
* @throws GraphQlInputException
116+
* @throws NoSuchEntityException
117+
*/
118+
private function updateCartCurrency(Quote $cart, int $storeId)
119+
{
120+
$cartStore = $this->storeRepository->getById($cart->getStoreId());
121+
$currentCartCurrencyCode = $cartStore->getCurrentCurrency()->getCode();
122+
if ((int)$cart->getStoreId() !== $storeId) {
123+
$newStore = $this->storeRepository->getById($storeId);
124+
if ($cartStore->getWebsite() !== $newStore->getWebsite()) {
125+
throw new GraphQlInputException(
126+
__('Can\'t assign cart to store in different website.')
127+
);
128+
}
129+
$cart->setStoreId($storeId);
130+
$cart->setStoreCurrencyCode($newStore->getCurrentCurrency());
131+
$cart->setQuoteCurrencyCode($newStore->getCurrentCurrency());
132+
} elseif ($cart->getQuoteCurrencyCode() !== $currentCartCurrencyCode) {
133+
$cart->setQuoteCurrencyCode($cartStore->getCurrentCurrency());
134+
} else {
135+
return;
136+
}
137+
$this->cartRepository->save($cart);
138+
}
104139
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@
1111
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1212
use Magento\GraphQl\Model\Query\ContextInterface;
1313
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1415
use Magento\Quote\Model\QuoteRepository;
1516

1617
/**
1718
* Set single shipping address for a specified shopping cart
1819
*/
1920
class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
2021
{
22+
/**
23+
* @var QuoteIdToMaskedQuoteIdInterface
24+
*/
25+
private $quoteIdToMaskedQuoteId;
26+
27+
/**
28+
* @var GetCartForUser
29+
*/
30+
private $getCartForUser;
31+
2132
/**
2233
* @var AssignShippingAddressToCart
2334
*/
@@ -34,15 +45,21 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
3445
private $quoteRepository;
3546

3647
/**
48+
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
49+
* @param GetCartForUser $getCartForUser
3750
* @param AssignShippingAddressToCart $assignShippingAddressToCart
3851
* @param GetShippingAddress $getShippingAddress
3952
* @param QuoteRepository|null $quoteRepository
4053
*/
4154
public function __construct(
55+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId,
56+
GetCartForUser $getCartForUser,
4257
AssignShippingAddressToCart $assignShippingAddressToCart,
4358
GetShippingAddress $getShippingAddress,
4459
QuoteRepository $quoteRepository = null
4560
) {
61+
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
62+
$this->getCartForUser = $getCartForUser;
4663
$this->assignShippingAddressToCart = $assignShippingAddressToCart;
4764
$this->getShippingAddress = $getShippingAddress;
4865
$this->quoteRepository = $quoteRepository
@@ -81,7 +98,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
8198
throw $e;
8299
}
83100
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
84-
// trigger quote re-evaluation after address change
101+
102+
// reload updated cart & trigger quote re-evaluation after address change
103+
$maskedId = $this->quoteIdToMaskedQuoteId->execute((int)$cart->getId());
104+
$cart = $this->getCartForUser->execute($maskedId, $context->getUserId(), $cart->getStoreId());
85105
$this->quoteRepository->save($cart);
86106
}
87107
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6363
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
6464
$this->addProductsToCart->execute($cart, $cartItems);
6565

66+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
6667
return [
6768
'cart' => [
6869
'model' => $cart,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8585
throw new LocalizedException(__($e->getMessage()), $e);
8686
}
8787

88+
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);
8889
return [
8990
'cart' => [
9091
'model' => $cart,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6060
return [
6161
'price' => [
6262
'currency' => $currencyCode,
63-
'value' => $cartItem->getPrice(),
63+
'value' => $cartItem->getCalculationPrice(),
6464
],
6565
'row_total' => [
6666
'currency' => $currencyCode,

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1717
use Magento\Quote\Api\CartItemRepositoryInterface;
18+
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
1819
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1920
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
2021

@@ -33,6 +34,11 @@ class RemoveItemFromCart implements ResolverInterface
3334
*/
3435
private $cartItemRepository;
3536

37+
/**
38+
* @var MaskedQuoteIdToQuoteId
39+
*/
40+
private $maskedQuoteIdToQuoteId;
41+
3642
/**
3743
* @var ArgumentsProcessorInterface
3844
*/
@@ -41,15 +47,18 @@ class RemoveItemFromCart implements ResolverInterface
4147
/**
4248
* @param GetCartForUser $getCartForUser
4349
* @param CartItemRepositoryInterface $cartItemRepository
50+
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
4451
* @param ArgumentsProcessorInterface $argsSelection
4552
*/
4653
public function __construct(
4754
GetCartForUser $getCartForUser,
4855
CartItemRepositoryInterface $cartItemRepository,
56+
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
4957
ArgumentsProcessorInterface $argsSelection
5058
) {
5159
$this->getCartForUser = $getCartForUser;
5260
$this->cartItemRepository = $cartItemRepository;
61+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
5362
$this->argsSelection = $argsSelection;
5463
}
5564

@@ -63,23 +72,30 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6372
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
6473
}
6574
$maskedCartId = $processedArgs['input']['cart_id'];
75+
try {
76+
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
77+
} catch (NoSuchEntityException $exception) {
78+
throw new GraphQlNoSuchEntityException(
79+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
80+
);
81+
}
6682

6783
if (empty($processedArgs['input']['cart_item_id'])) {
6884
throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing.'));
6985
}
7086
$itemId = $processedArgs['input']['cart_item_id'];
7187

7288
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
73-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7489

7590
try {
76-
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
91+
$this->cartItemRepository->deleteById($cartId, $itemId);
7792
} catch (NoSuchEntityException $e) {
7893
throw new GraphQlNoSuchEntityException(__('The cart doesn\'t contain the item'));
7994
} catch (LocalizedException $e) {
8095
throw new GraphQlInputException(__($e->getMessage()), $e);
8196
}
8297

98+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
8399
return [
84100
'cart' => [
85101
'model' => $cart,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setBillingAddressOnCart->execute($context, $cart, $billingAddress);
72+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7273

7374
return [
7475
'cart' => [

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setPaymentMethodOnCart->execute($cart, $paymentData);
72+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7273

7374
return [
7475
'cart' => [

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
72+
// reload updated cart
73+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7274

7375
return [
7476
'cart' => [

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setShippingMethodsOnCart->execute($context, $cart, $shippingMethods);
72+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7273

7374
return [
7475
'cart' => [

0 commit comments

Comments
 (0)