Skip to content

Commit f6a7cc4

Browse files
author
Prabhu Ram
committed
PWA-1379: [GraphQl] AddWishlistItemsToCartOutput.status doesn't return the correct status if items are not added to cart
- Added wishlist id and item id in error response
1 parent 2504952 commit f6a7cc4

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ public function resolve(
168168
/** @var AddProductsToCartOutput $addProductsToCartOutput */
169169
$addProductsToCartOutput = $this->addProductsToCartService->execute($maskedCartId, [$cartItem]);
170170
$errors = array_map(
171-
function (Error $error) {
171+
function (Error $error) use ($item, $wishlist) {
172172
return [
173+
'wishlistItemId' => $item->getID(),
174+
'wishlistId' => $wishlist->getId(),
173175
'code' => $error->getCode(),
174176
'message' => $error->getMessage(),
175177
];

app/code/Magento/WishlistGraphQl/etc/schema.graphqls

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,21 @@ type Mutation {
6969
type AddWishlistItemsToCartOutput {
7070
wishlist: Wishlist! @doc(description: "Contains the wish list with all items that were successfully added")
7171
status: Boolean! @doc(description: "Indicates whether the attempt to add items to the customer's cart was successful")
72-
add_wishlist_items_to_cart_user_errors: [CartUserInputError!]! @doc(description: "An array of errors encountered while adding products to the customer's cart")
72+
add_wishlist_items_to_cart_user_errors: [WishlistCartUserInputError!]! @doc(description: "An array of errors encountered while adding products to the customer's cart")
73+
}
74+
75+
type WishlistCartUserInputError {
76+
message: String! @doc(description: "A localized error message")
77+
code: WishlistCartUserInputErrorType! @doc(description: "Wishlist-Cart-specific error code")
78+
wishlistId: ID! @doc(description: "The id of the wishlist associated with the error")
79+
wishlistItemId: ID! @doc(description: "The id of the wishlist item associated with the error")
80+
}
81+
82+
enum WishlistCartUserInputErrorType {
83+
PRODUCT_NOT_FOUND
84+
NOT_SALABLE
85+
INSUFFICIENT_STOCK
86+
UNDEFINED
7387
}
7488

7589
input WishlistItemInput @doc(description: "Defines the items to add to a wish list") {

dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddWishlistItemsToCartTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,35 @@ public function testAddItemsToCart(): void
5656
$this->assertEquals($response['addWishlistItemsToCart']['status'], true);
5757
}
5858

59+
/**
60+
* @magentoConfigFixture default_store wishlist/general/active 1
61+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
62+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_configurable_product.php
63+
*/
64+
public function testAddIncompleteItemsToCart(): void
65+
{
66+
$wishlist = $this->getWishlist();
67+
$customerWishlist = $wishlist['customer']['wishlists'][0];
68+
$wishlistId = $customerWishlist['id'];
69+
$wishlistItem = $customerWishlist['items_v2']['items'][0];
70+
$itemId = $wishlistItem['id'];
71+
72+
$query = $this->getQuery($wishlistId, $itemId);
73+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
74+
75+
$this->assertArrayHasKey('addWishlistItemsToCart', $response);
76+
$wishlistAfterAddingToCart = $response['addWishlistItemsToCart']['wishlist'];
77+
$userErrors = $response['addWishlistItemsToCart']['add_wishlist_items_to_cart_user_errors'];
78+
$this->assertEquals($userErrors[0]['message'], 'You need to choose options for your item.');
79+
$this->assertEquals($userErrors[0]['code'], 'UNDEFINED');
80+
$this->assertEquals($userErrors[0]['wishlistId'], $wishlistId);
81+
$this->assertEquals($userErrors[0]['wishlistItemId'], $itemId);
82+
$wishlistItems = $wishlistAfterAddingToCart['items_v2']['items'];
83+
$this->assertNotEmpty($wishlistItems);
84+
$this->assertArrayHasKey('status', $response['addWishlistItemsToCart']);
85+
$this->assertEquals($response['addWishlistItemsToCart']['status'], false);
86+
}
87+
5988
/**
6089
* @magentoConfigFixture default_store wishlist/general/active 1
6190
* @magentoApiDataFixture Magento/Customer/_files/customer.php
@@ -255,6 +284,8 @@ private function getQuery(
255284
add_wishlist_items_to_cart_user_errors{
256285
message
257286
code
287+
wishlistId
288+
wishlistItemId
258289
}
259290
}
260291
}

0 commit comments

Comments
 (0)