Skip to content

Commit a93cf50

Browse files
committed
Merge remote-tracking branch 'origin/AC-14464' into spartans_pr_29102025
2 parents 7fa400a + 41fa0dd commit a93cf50

File tree

3 files changed

+1069
-23
lines changed

3 files changed

+1069
-23
lines changed

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

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,33 @@
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;
1821
use Magento\Quote\Model\Cart\Data\Error;
1922
use Magento\WishlistGraphQl\Mapper\WishlistDataMapper;
2023
use Magento\WishlistGraphQl\Model\CartItems\CartItemsRequestBuilder;
21-
use Magento\Wishlist\Model\LocaleQuantityProcessor;
2224
use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemsCollection;
2325
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
2426
use Magento\Wishlist\Model\Wishlist;
2527
use Magento\Wishlist\Model\WishlistFactory;
26-
use Magento\Wishlist\Model\Wishlist\AddProductsToWishlist as AddProductsToWishlistModel;
2728
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
2829

2930
/**
3031
* Adding products to wishlist resolver
32+
*
33+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3134
*/
3235
class AddToCart implements ResolverInterface
3336
{
34-
/**
35-
* @var AddProductsToWishlistModel
36-
*/
37-
private $addProductsToWishlist;
38-
3937
/**
4038
* @var WishlistDataMapper
4139
*/
@@ -56,11 +54,6 @@ class AddToCart implements ResolverInterface
5654
*/
5755
private $wishlistFactory;
5856

59-
/**
60-
* @var LocaleQuantityProcessor
61-
*/
62-
private $quantityProcessor;
63-
6457
/**
6558
* @var CreateEmptyCartForCustomer
6659
*/
@@ -76,41 +69,53 @@ class AddToCart implements ResolverInterface
7669
*/
7770
private $cartItemsRequestBuilder;
7871

72+
/**
73+
* @var CartRepositoryInterface
74+
*/
75+
private $cartRepository;
76+
77+
/**
78+
* @var MaskedQuoteIdToQuoteIdInterface
79+
*/
80+
private $maskedQuoteIdToQuoteId;
81+
7982
/**
8083
* @param WishlistResourceModel $wishlistResource
8184
* @param WishlistFactory $wishlistFactory
8285
* @param WishlistConfig $wishlistConfig
83-
* @param AddProductsToWishlistModel $addProductsToWishlist
8486
* @param WishlistDataMapper $wishlistDataMapper
85-
* @param LocaleQuantityProcessor $quantityProcessor
8687
* @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
8788
* @param AddProductsToCartService $addProductsToCart
8889
* @param CartItemsRequestBuilder $cartItemsRequestBuilder
90+
* @param CartRepositoryInterface $cartRepository
91+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
8992
*/
9093
public function __construct(
9194
WishlistResourceModel $wishlistResource,
9295
WishlistFactory $wishlistFactory,
9396
WishlistConfig $wishlistConfig,
94-
AddProductsToWishlistModel $addProductsToWishlist,
9597
WishlistDataMapper $wishlistDataMapper,
96-
LocaleQuantityProcessor $quantityProcessor,
9798
CreateEmptyCartForCustomer $createEmptyCartForCustomer,
9899
AddProductsToCartService $addProductsToCart,
99-
CartItemsRequestBuilder $cartItemsRequestBuilder
100+
CartItemsRequestBuilder $cartItemsRequestBuilder,
101+
CartRepositoryInterface $cartRepository,
102+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
100103
) {
101104
$this->wishlistResource = $wishlistResource;
102105
$this->wishlistFactory = $wishlistFactory;
103106
$this->wishlistConfig = $wishlistConfig;
104-
$this->addProductsToWishlist = $addProductsToWishlist;
105107
$this->wishlistDataMapper = $wishlistDataMapper;
106-
$this->quantityProcessor = $quantityProcessor;
107108
$this->createEmptyCartForCustomer = $createEmptyCartForCustomer;
108109
$this->addProductsToCartService = $addProductsToCart;
109110
$this->cartItemsRequestBuilder = $cartItemsRequestBuilder;
111+
$this->cartRepository = $cartRepository;
112+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
110113
}
111114

112115
/**
113116
* @inheritdoc
117+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
118+
* @SuppressWarnings(PHPMD.NPathComplexity)
114119
*/
115120
public function resolve(
116121
Field $field,
@@ -151,7 +156,9 @@ public function resolve(
151156
if (!empty($itemIds)) {
152157
$unknownItemIds = array_diff($itemIds, array_keys($collection->getItems()));
153158
if (!empty($unknownItemIds)) {
154-
throw new GraphQlInputException(__('The wishlist item ids "'.implode(',', $unknownItemIds).'" were not found.'));
159+
throw new GraphQlInputException(
160+
__('The wishlist item ids "'.implode(',', $unknownItemIds).'" were not found.')
161+
);
155162
}
156163
}
157164
$maskedCartId = $this->createEmptyCartForCustomer->execute($customerId);
@@ -183,7 +190,10 @@ function (Error $error) use ($item, $wishlist) {
183190
$item->delete();
184191
$addedProducts[] = $item->getProductId();
185192
}
186-
$cartErrors = array_merge($cartErrors, $errors);
193+
if (!empty($errors)) {
194+
$this->saveCart($maskedCartId);
195+
}
196+
$cartErrors = [...$cartErrors, ...$errors];
187197
}
188198
if (!empty($addedProducts)) {
189199
$wishlist->save();
@@ -219,9 +229,11 @@ private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist
219229
/**
220230
* Get customer wishlist items
221231
*
232+
* @param Wishlist $wishlist
222233
* @param array $itemIds
223234
*
224235
* @return WishlistItemsCollection
236+
* @throws NoSuchEntityException
225237
*/
226238
private function getWishlistItems(Wishlist $wishlist, array $itemIds): WishlistItemsCollection
227239
{
@@ -233,4 +245,22 @@ private function getWishlistItems(Wishlist $wishlist, array $itemIds): WishlistI
233245
}
234246
return $collection;
235247
}
248+
249+
/**
250+
* Save cart on error while adding wishlist product to cart
251+
*
252+
* @param string $maskedCartId
253+
* @return void
254+
* @throws GraphQlInputException
255+
*/
256+
private function saveCart(string $maskedCartId): void
257+
{
258+
try {
259+
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
260+
$cart = $this->cartRepository->get($cartId);
261+
$this->cartRepository->save($cart);
262+
} catch (NoSuchEntityException $e) {
263+
throw new GraphQlInputException(__('The wishlist could not be saved.'));
264+
}
265+
}
236266
}

0 commit comments

Comments
 (0)