Skip to content

Commit 2dbf838

Browse files
committed
MC-19194: UpdateCartItems mutation does not update cart item quantity
- fix performance issue
1 parent 9532e28 commit 2dbf838

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ public function execute(Quote $cart, int $cartItemId, float $quantity, array $cu
9393
)
9494
);
9595
}
96-
97-
$this->quoteRepository->save($cart);
9896
}
9997

10098
/**
@@ -105,7 +103,7 @@ public function execute(Quote $cart, int $cartItemId, float $quantity, array $cu
105103
* @param float $quantity
106104
* @throws GraphQlNoSuchEntityException
107105
* @throws NoSuchEntityException
108-
* @throws GraphQlNoSuchEntityException
106+
* @throws GraphQlInputException
109107
*/
110108
private function updateItemQuantity(int $itemId, Quote $cart, float $quantity)
111109
{
@@ -117,7 +115,6 @@ private function updateItemQuantity(int $itemId, Quote $cart, float $quantity)
117115
}
118116
$cartItem->setQty($quantity);
119117
$this->validateCartItem($cartItem);
120-
$this->cartItemRepository->save($cartItem);
121118
}
122119

123120
/**

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

Lines changed: 11 additions & 1 deletion
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\Api\CartRepositoryInterface;
1819
use Magento\Quote\Model\Quote;
1920
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
2021
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem;
@@ -39,19 +40,27 @@ class UpdateCartItems implements ResolverInterface
3940
*/
4041
private $cartItemRepository;
4142

43+
/**
44+
* @var CartRepositoryInterface
45+
*/
46+
private $cartRepository;
47+
4248
/**
4349
* @param GetCartForUser $getCartForUser
4450
* @param CartItemRepositoryInterface $cartItemRepository
4551
* @param UpdateCartItem $updateCartItem
52+
* @param CartRepositoryInterface $cartRepository
4653
*/
4754
public function __construct(
4855
GetCartForUser $getCartForUser,
4956
CartItemRepositoryInterface $cartItemRepository,
50-
UpdateCartItem $updateCartItem
57+
UpdateCartItem $updateCartItem,
58+
CartRepositoryInterface $cartRepository
5159
) {
5260
$this->getCartForUser = $getCartForUser;
5361
$this->cartItemRepository = $cartItemRepository;
5462
$this->updateCartItem = $updateCartItem;
63+
$this->cartRepository = $cartRepository;
5564
}
5665

5766
/**
@@ -76,6 +85,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7685

7786
try {
7887
$this->processCartItems($cart, $cartItems);
88+
$this->cartRepository->save($cart);
7989
} catch (NoSuchEntityException $e) {
8090
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
8191
} catch (LocalizedException $e) {

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function testUpdateCartItemQuantity()
7171
$this->assertEquals($itemId, $item['id']);
7272
$this->assertEquals($quantity, $item['quantity']);
7373

74+
//Check that update is correctly reflected in cart
7475
$cartQuery = $this->getCartQuery($maskedQuoteId);
7576
$response = $this->graphQlQuery($cartQuery);
7677

@@ -101,6 +102,15 @@ public function testRemoveCartItemIfQuantityIsZero()
101102

102103
$responseCart = $response['updateCartItems']['cart'];
103104
$this->assertCount(0, $responseCart['items']);
105+
106+
//Check that update is correctly reflected in cart
107+
$cartQuery = $this->getCartQuery($maskedQuoteId);
108+
$response = $this->graphQlQuery($cartQuery);
109+
110+
$this->assertArrayHasKey('cart', $response);
111+
112+
$responseCart = $response['cart'];
113+
$this->assertCount(0, $responseCart['items']);
104114
}
105115

106116
/**

0 commit comments

Comments
 (0)