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 */
66declare (strict_types = 1 );
77
88namespace Magento \WishlistGraphQl \Model \Resolver \Wishlist ;
99
10+ use Magento \Framework \Exception \NoSuchEntityException ;
1011use Magento \Framework \GraphQl \Config \Element \Field ;
1112use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
1213use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
1314use Magento \Framework \GraphQl \Query \ResolverInterface ;
1415use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
16+ use Magento \Quote \Api \CartRepositoryInterface ;
17+ use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
1518use Magento \QuoteGraphQl \Model \Cart \CreateEmptyCartForCustomer ;
1619use Magento \Quote \Model \Cart \AddProductsToCart as AddProductsToCartService ;
1720use 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