Skip to content

Commit 43f5935

Browse files
committed
AC-14464: GraphQL addWishlistItemsToCart Fails to Update Quantity for Existing Cart Items When One Wishlist Item Is Invalid (Magento 2.4.7-p3)
Updated the resolver to save cart after each loop of adding wishlist item to cart Added WebAPI and Unit tests coverage
1 parent 4baea6d commit 43f5935

File tree

3 files changed

+1074
-7
lines changed

3 files changed

+1074
-7
lines changed

app/code/Magento/WishlistGraphQl/Model/Resolver/Wishlist/AddToCart.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2021 Adobe
4+
* All Rights Reserved.
55
*/
66
declare (strict_types = 1);
77

88
namespace Magento\WishlistGraphQl\Model\Resolver\Wishlist;
99

10+
use Magento\Framework\Exception\NoSuchEntityException;
1011
use Magento\Framework\GraphQl\Config\Element\Field;
1112
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1314
use Magento\Framework\GraphQl\Query\ResolverInterface;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1518
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForCustomer;
1619
use Magento\Quote\Model\Cart\AddProductsToCart as AddProductsToCartService;
1720
use Magento\Quote\Model\Cart\Data\CartItemFactory;
@@ -76,6 +79,16 @@ class AddToCart implements ResolverInterface
7679
*/
7780
private $cartItemsRequestBuilder;
7881

82+
/**
83+
* @var CartRepositoryInterface
84+
*/
85+
private $cartRepository;
86+
87+
/**
88+
* @var MaskedQuoteIdToQuoteIdInterface
89+
*/
90+
private $maskedQuoteIdToQuoteId;
91+
7992
/**
8093
* @param WishlistResourceModel $wishlistResource
8194
* @param WishlistFactory $wishlistFactory
@@ -86,6 +99,8 @@ class AddToCart implements ResolverInterface
8699
* @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
87100
* @param AddProductsToCartService $addProductsToCart
88101
* @param CartItemsRequestBuilder $cartItemsRequestBuilder
102+
* @param CartRepositoryInterface $cartRepository
103+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
89104
*/
90105
public function __construct(
91106
WishlistResourceModel $wishlistResource,
@@ -96,7 +111,9 @@ public function __construct(
96111
LocaleQuantityProcessor $quantityProcessor,
97112
CreateEmptyCartForCustomer $createEmptyCartForCustomer,
98113
AddProductsToCartService $addProductsToCart,
99-
CartItemsRequestBuilder $cartItemsRequestBuilder
114+
CartItemsRequestBuilder $cartItemsRequestBuilder,
115+
CartRepositoryInterface $cartRepository,
116+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
100117
) {
101118
$this->wishlistResource = $wishlistResource;
102119
$this->wishlistFactory = $wishlistFactory;
@@ -107,6 +124,8 @@ public function __construct(
107124
$this->createEmptyCartForCustomer = $createEmptyCartForCustomer;
108125
$this->addProductsToCartService = $addProductsToCart;
109126
$this->cartItemsRequestBuilder = $cartItemsRequestBuilder;
127+
$this->cartRepository = $cartRepository;
128+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
110129
}
111130

112131
/**
@@ -151,7 +170,9 @@ public function resolve(
151170
if (!empty($itemIds)) {
152171
$unknownItemIds = array_diff($itemIds, array_keys($collection->getItems()));
153172
if (!empty($unknownItemIds)) {
154-
throw new GraphQlInputException(__('The wishlist item ids "'.implode(',', $unknownItemIds).'" were not found.'));
173+
throw new GraphQlInputException(
174+
__('The wishlist item ids "'.implode(',', $unknownItemIds).'" were not found.')
175+
);
155176
}
156177
}
157178
$maskedCartId = $this->createEmptyCartForCustomer->execute($customerId);
@@ -183,7 +204,10 @@ function (Error $error) use ($item, $wishlist) {
183204
$item->delete();
184205
$addedProducts[] = $item->getProductId();
185206
}
186-
$cartErrors = array_merge($cartErrors, $errors);
207+
if (!empty($errors)) {
208+
$this->saveCart($maskedCartId);
209+
}
210+
$cartErrors = [...$cartErrors, ...$errors];
187211
}
188212
if (!empty($addedProducts)) {
189213
$wishlist->save();
@@ -219,9 +243,11 @@ private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist
219243
/**
220244
* Get customer wishlist items
221245
*
246+
* @param Wishlist $wishlist
222247
* @param array $itemIds
223248
*
224249
* @return WishlistItemsCollection
250+
* @throws NoSuchEntityException
225251
*/
226252
private function getWishlistItems(Wishlist $wishlist, array $itemIds): WishlistItemsCollection
227253
{
@@ -233,4 +259,22 @@ private function getWishlistItems(Wishlist $wishlist, array $itemIds): WishlistI
233259
}
234260
return $collection;
235261
}
262+
263+
/**
264+
* Save cart on error while adding wishlist product to cart
265+
*
266+
* @param string $maskedCartId
267+
* @return void
268+
* @throws GraphQlInputException
269+
*/
270+
private function saveCart(string $maskedCartId): void
271+
{
272+
try {
273+
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
274+
$cart = $this->cartRepository->get($cartId);
275+
$this->cartRepository->save($cart);
276+
} catch (NoSuchEntityException $e) {
277+
throw new GraphQlInputException(__('The wishlist could not be saved.'));
278+
}
279+
}
236280
}

0 commit comments

Comments
 (0)