Skip to content

Commit 10dc15f

Browse files
committed
MC-37484: Cart query error when trying to switch store view
1 parent 18c0196 commit 10dc15f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
118118
private function updateCartCurrency(Quote $cart, int $storeId)
119119
{
120120
$cartStore = $this->storeRepository->getById($cart->getStoreId());
121+
$currentCartCurrencyCode = $cartStore->getCurrentCurrency()->getCode();
121122
if ((int)$cart->getStoreId() !== $storeId) {
122123
$newStore = $this->storeRepository->getById($storeId);
123124
if ($cartStore->getWebsite() !== $newStore->getWebsite()) {
@@ -128,8 +129,10 @@ private function updateCartCurrency(Quote $cart, int $storeId)
128129
$cart->setStoreId($storeId);
129130
$cart->setStoreCurrencyCode($newStore->getCurrentCurrency());
130131
$cart->setQuoteCurrencyCode($newStore->getCurrentCurrency());
131-
} else {
132+
} elseif ($cart->getQuoteCurrencyCode() !== $currentCartCurrencyCode) {
132133
$cart->setQuoteCurrencyCode($cartStore->getCurrentCurrency());
134+
} else {
135+
return;
133136
}
134137
$this->cartRepository->save($cart);
135138
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,34 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Quote\Model\Cart\Totals;
1415
use Magento\Quote\Model\Quote\Item;
16+
use Magento\Quote\Model\Quote\TotalsCollector;
1517

1618
/**
1719
* @inheritdoc
1820
*/
1921
class CartItemPrices implements ResolverInterface
2022
{
23+
/**
24+
* @var TotalsCollector
25+
*/
26+
private $totalsCollector;
27+
28+
/**
29+
* @var Totals
30+
*/
31+
private $totals;
32+
33+
/**
34+
* @param TotalsCollector $totalsCollector
35+
*/
36+
public function __construct(
37+
TotalsCollector $totalsCollector
38+
) {
39+
$this->totalsCollector = $totalsCollector;
40+
}
41+
2142
/**
2243
* @inheritdoc
2344
*/
@@ -28,6 +49,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
2849
}
2950
/** @var Item $cartItem */
3051
$cartItem = $value['model'];
52+
53+
if (!$this->totals) {
54+
// The totals calculation is based on quote address.
55+
// But the totals should be calculated even if no address is set
56+
$this->totals = $this->totalsCollector->collectQuoteTotals($cartItem->getQuote());
57+
}
3158
$currencyCode = $cartItem->getQuote()->getQuoteCurrencyCode();
3259

3360
return [

0 commit comments

Comments
 (0)