Skip to content

Commit bc9567f

Browse files
committed
#31331:[GraphQl] Add wishlist item to cart Implementation
1 parent 7b66243 commit bc9567f

File tree

6 files changed

+55
-89
lines changed

6 files changed

+55
-89
lines changed

app/code/Magento/WishlistGraphQl/Mapper/WishlistDataMapper.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
use Magento\Framework\GraphQl\Schema\Type\Enum\DataMapperInterface;
1111
use Magento\Wishlist\Model\Wishlist;
12-
use Magento\Framework\App\ObjectManager;
13-
use Magento\Framework\GraphQl\Query\Uid;
1412

1513
/**
1614
* Prepares the wishlist output as associative array
@@ -22,22 +20,13 @@ class WishlistDataMapper
2220
*/
2321
private $enumDataMapper;
2422

25-
/**
26-
* @var Uid
27-
*/
28-
private $uidEncoder;
29-
3023
/**
3124
* @param DataMapperInterface $enumDataMapper
32-
* @param Uid|null $uidEncoder
3325
*/
3426
public function __construct(
35-
DataMapperInterface $enumDataMapper,
36-
Uid $uidEncoder = null
27+
DataMapperInterface $enumDataMapper
3728
) {
3829
$this->enumDataMapper = $enumDataMapper;
39-
$this->uidEncoder = $uidEncoder ?: ObjectManager::getInstance()
40-
->get(Uid::class);
4130
}
4231

4332
/**
@@ -51,7 +40,6 @@ public function map(Wishlist $wishlist): array
5140
{
5241
return [
5342
'id' => $wishlist->getId(),
54-
'uid' => $this->uidEncoder->encode($wishlist->getId()),
5543
'sharing_code' => $wishlist->getSharingCode(),
5644
'updated_at' => $wishlist->getUpdatedAt(),
5745
'items_count' => $wishlist->getItemsCount(),

app/code/Magento/WishlistGraphQl/Model/CartItems/CartItemsRequestBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public function build(Item $wishlistItem): array
5353
foreach ($this->providers as $provider) {
5454
$cartItems = array_merge_recursive($cartItems, $provider->execute($wishlistItem, $parentsku));
5555
}
56-
return [$cartItems];
56+
return $cartItems;
5757
}
5858
}

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

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
14-
use Magento\Framework\GraphQl\Query\Uid;
1514
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1615
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForCustomer;
1716
use Magento\Quote\Model\Cart\AddProductsToCart as AddProductsToCartService;
@@ -77,11 +76,6 @@ class AddToCart implements ResolverInterface
7776
*/
7877
private $cartItemsRequestBuilder;
7978

80-
/**
81-
* @var Uid
82-
*/
83-
private $uidEncoder;
84-
8579
/**
8680
* @param WishlistResourceModel $wishlistResource
8781
* @param WishlistFactory $wishlistFactory
@@ -91,7 +85,6 @@ class AddToCart implements ResolverInterface
9185
* @param LocaleQuantityProcessor $quantityProcessor
9286
* @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
9387
* @param CartItemsRequestBuilder $cartItemsRequestBuilder
94-
* @param Uid $uidEncoder
9588
*/
9689
public function __construct(
9790
WishlistResourceModel $wishlistResource,
@@ -102,8 +95,7 @@ public function __construct(
10295
LocaleQuantityProcessor $quantityProcessor,
10396
CreateEmptyCartForCustomer $createEmptyCartForCustomer,
10497
AddProductsToCartService $addProductsToCart,
105-
CartItemsRequestBuilder $cartItemsRequestBuilder,
106-
Uid $uidEncoder
98+
CartItemsRequestBuilder $cartItemsRequestBuilder
10799
) {
108100
$this->wishlistResource = $wishlistResource;
109101
$this->wishlistFactory = $wishlistFactory;
@@ -114,7 +106,6 @@ public function __construct(
114106
$this->createEmptyCartForCustomer = $createEmptyCartForCustomer;
115107
$this->addProductsToCartService = $addProductsToCart;
116108
$this->cartItemsRequestBuilder = $cartItemsRequestBuilder;
117-
$this->uidEncoder = $uidEncoder;
118109
}
119110

120111
/**
@@ -138,55 +129,59 @@ public function resolve(
138129
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
139130
}
140131

141-
if (empty($args['wishlistUid'])) {
142-
throw new GraphQlInputException(__('"wishlistUid" value should be specified'));
132+
if (empty($args['wishlistId'])) {
133+
throw new GraphQlInputException(__('"wishlistId" value should be specified'));
143134
}
144-
$wishlistId = (int) $this->uidEncoder->decode($args['wishlistUid']);
135+
$wishlistId = (int) $args['wishlistId'];
145136
$wishlist = $this->getWishlist($wishlistId, $customerId);
137+
$isOwner = $wishlist->isOwner($customerId);
146138

147139
if (null === $wishlist->getId() || $customerId !== (int) $wishlist->getCustomerId()) {
148140
throw new GraphQlInputException(__('The wishlist was not found.'));
149141
}
150142

151143
$itemIds = [];
152-
if (isset($args['wishlistItemUids'])) {
153-
$itemIds = array_map(
154-
function ($id) {
155-
return $this->uidEncoder->decode($id);
156-
},
157-
$args['wishlistItemUids']
158-
);
144+
if (isset($args['wishlistItemIds'])) {
145+
$itemIds = $args['wishlistItemIds'];
159146
}
160147

161148
$collection = $this->getWishlistItems($wishlist, $itemIds);
162149

163150
$maskedCartId = $this->createEmptyCartForCustomer->execute($customerId);
164151

165-
$cartItems = [];
152+
$cartErrors = [];
153+
$addedProducts = [];
154+
$errors = [];
166155
foreach ($collection as $item) {
167156
$disableAddToCart = $item->getProduct()->getDisableAddToCart();
168157
$item->getProduct()->setDisableAddToCart($disableAddToCart);
169-
$cartItemsData = $this->cartItemsRequestBuilder->build($item);
170-
foreach ($cartItemsData as $cartItemData) {
171-
$cartItems[] = (new CartItemFactory())->create($cartItemData);
172-
}
173-
}
174158

175-
/** @var AddProductsToCartOutput $addProductsToCartOutput */
176-
$addProductsToCartOutput = $this->addProductsToCartService->execute($maskedCartId, $cartItems);
159+
$cartItemData = $this->cartItemsRequestBuilder->build($item);
160+
$cartItem = (new CartItemFactory())->create($cartItemData);
177161

178-
return [
179-
'status' => !$addProductsToCartOutput->getCart()->hasError(),
180-
'add_wishlist_items_to_cart_user_errors' => array_map(
162+
/** @var AddProductsToCartOutput $addProductsToCartOutput */
163+
$addProductsToCartOutput = $this->addProductsToCartService->execute($maskedCartId, [$cartItem]);
164+
$errors = array_map(
181165
function (Error $error) {
182166
return [
183167
'code' => $error->getCode(),
184168
'message' => $error->getMessage(),
185-
'path' => [$error->getCartItemPosition()],
186169
];
187170
},
188171
$addProductsToCartOutput->getErrors()
189-
),
172+
);
173+
if ($isOwner && empty($errors)) {
174+
$item->delete();
175+
$addedProducts[] = $item->getProductId();
176+
}
177+
$cartErrors = array_merge($cartErrors, $errors);
178+
}
179+
if (!empty($addedProducts)) {
180+
$wishlist->save();
181+
}
182+
return [
183+
'status' => isset($cartErrors) ? false : true,
184+
'add_wishlist_items_to_cart_user_errors' => $cartErrors,
190185
];
191186
}
192187

app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistItems.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
13-
use Magento\Framework\App\ObjectManager;
14-
use Magento\Framework\GraphQl\Query\Uid;
1513
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1614
use Magento\Store\Api\Data\StoreInterface;
1715
use Magento\Store\Model\StoreManagerInterface;
@@ -35,25 +33,16 @@ class WishlistItems implements ResolverInterface
3533
*/
3634
private $storeManager;
3735

38-
/**
39-
* @var Uid
40-
*/
41-
private $uidEncoder;
42-
4336
/**
4437
* @param WishlistItemCollectionFactory $wishlistItemCollectionFactory
4538
* @param StoreManagerInterface $storeManager
46-
* @param Uid|null $uidEncoder
4739
*/
4840
public function __construct(
4941
WishlistItemCollectionFactory $wishlistItemCollectionFactory,
50-
StoreManagerInterface $storeManager,
51-
Uid $uidEncoder = null
42+
StoreManagerInterface $storeManager
5243
) {
5344
$this->wishlistItemCollectionFactory = $wishlistItemCollectionFactory;
5445
$this->storeManager = $storeManager;
55-
$this->uidEncoder = $uidEncoder ?: ObjectManager::getInstance()
56-
->get(Uid::class);
5746
}
5847

5948
/**
@@ -80,7 +69,6 @@ public function resolve(
8069
foreach ($wishlistItems as $wishlistItem) {
8170
$data[] = [
8271
'id' => $wishlistItem->getId(),
83-
'uid' => $this->uidEncoder->encode($wishlistItem->getId()),
8472
'quantity' => $wishlistItem->getData('qty'),
8573
'description' => $wishlistItem->getDescription(),
8674
'added_at' => $wishlistItem->getAddedAt(),

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ type WishlistOutput @doc(description: "Deprecated: `Wishlist` type should be use
2323
}
2424

2525
type Wishlist {
26-
id: ID @deprecated(reason: "Use `uid` instead") @doc(description: "The unique ID for a `Wishlist` object")
27-
uid: ID @doc(description: "The unique ID for a `Wishlist` object")
26+
id: ID @doc(description: "The unique ID for a `Wishlist` object")
2827
items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @deprecated(reason: "Use field `items_v2` from type `Wishlist` instead")
2928
items_v2(
3029
currentPage: Int = 1,
@@ -36,8 +35,7 @@ type Wishlist {
3635
}
3736

3837
interface WishlistItemInterface @typeResolver(class: "Magento\\WishlistGraphQl\\Model\\Resolver\\Type\\WishlistItemType") {
39-
id: ID! @deprecated(reason: "Use `uid` instead") @doc(description: "The unique ID for a `WishlistItemInterface` object")
40-
uid: ID! @doc(description: "The unique ID for a `WishlistItemInterface` object")
38+
id: ID! @doc(description: "The unique ID for a `WishlistItemInterface` object")
4139
quantity: Float! @doc(description: "The quantity of this wish list item")
4240
description: String @doc(description: "The description of the item")
4341
added_at: String! @doc(description: "The date and time the item was added to the wish list")
@@ -63,8 +61,8 @@ type Mutation {
6361
removeProductsFromWishlist(wishlistId: ID!, wishlistItemsIds: [ID!]!): RemoveProductsFromWishlistOutput @doc(description: "Removes one or more products from the specified wish list") @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\RemoveProductsFromWishlist")
6462
updateProductsInWishlist(wishlistId: ID!, wishlistItems: [WishlistItemUpdateInput!]!): UpdateProductsInWishlistOutput @doc(description: "Updates one or more products in the specified wish list") @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\UpdateProductsInWishlist")
6563
addWishlistItemsToCart(
66-
wishlistUid: ID!, @doc(description: "The unique ID of the requisition list")
67-
wishlistItemUids: [ID!] @doc(description: "An array of UIDs representing products to be added to the cart. If no UIDs are specified, all items in the wishlist will be added to the cart")
64+
wishlistId: ID!, @doc(description: "The unique ID of the requisition list")
65+
wishlistItemIds: [ID!] @doc(description: "An array of IDs representing products to be added to the cart. If no IDs are specified, all items in the wishlist will be added to the cart")
6866
): AddWishlistItemsToCartOutput @resolver(class: "Magento\\WishlistGraphQl\\Model\\Resolver\\Wishlist\\AddToCart") @doc(description: "Add items in the specified wishlist to the customer's cart")
6967
}
7068

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public function testAddItemsToCart(): void
4141
{
4242
$wishlist = $this->getWishlist();
4343
$customerWishlist = $wishlist['customer']['wishlists'][0];
44-
$wishlistUid = $customerWishlist['uid'];
44+
$wishlistId = $customerWishlist['id'];
4545
$wishlistItem = $customerWishlist['items_v2']['items'][0];
46-
$itemUid = $wishlistItem['uid'];
46+
$itemId = $wishlistItem['id'];
4747

48-
$query = $this->getQuery($wishlistUid, $itemUid);
48+
$query = $this->getQuery($wishlistId, $itemId);
4949
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
5050

5151
$this->assertArrayHasKey('addWishlistItemsToCart', $response);
@@ -65,11 +65,11 @@ public function testAddItemsToCartForInvalidUser(): void
6565

6666
$wishlist = $this->getWishlist();
6767
$customerWishlist = $wishlist['customer']['wishlists'][0];
68-
$wishlistUid = $customerWishlist['uid'];
68+
$wishlistId = $customerWishlist['id'];
6969
$wishlistItem = $customerWishlist['items_v2']['items'][0];
70-
$itemUid = $wishlistItem['uid'];
70+
$itemId = $wishlistItem['id'];
7171

72-
$query = $this->getQuery($wishlistUid, $itemUid);
72+
$query = $this->getQuery($wishlistId, $itemId);
7373
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@example.com', 'password'));
7474
}
7575

@@ -85,11 +85,11 @@ public function testAddItemsToCartForGuestUser(): void
8585

8686
$wishlist = $this->getWishlist();
8787
$customerWishlist = $wishlist['customer']['wishlists'][0];
88-
$wishlistUid = $customerWishlist['uid'];
88+
$wishlistId = $customerWishlist['id'];
8989
$wishlistItem = $customerWishlist['items_v2']['items'][0];
90-
$itemUid = $wishlistItem['uid'];
90+
$itemId = $wishlistItem['id'];
9191

92-
$query = $this->getQuery($wishlistUid, $itemUid);
92+
$query = $this->getQuery($wishlistId, $itemId);
9393

9494
$this->graphQlMutation($query, [], '', ['Authorization' => 'Bearer test_token']);
9595
}
@@ -102,14 +102,14 @@ public function testAddItemsToCartForGuestUser(): void
102102
public function testAddItemsToCartWithoutId(): void
103103
{
104104
$this->expectException(Exception::class);
105-
$this->expectExceptionMessage('"wishlistUid" value should be specified');
105+
$this->expectExceptionMessage('"wishlistId" value should be specified');
106106

107-
$wishlistUid = '';
107+
$wishlistId = '';
108108
$wishlist = $this->getWishlist();
109109
$customerWishlist = $wishlist['customer']['wishlists'][0];
110110
$wishlistItem = $customerWishlist['items_v2']['items'][0];
111-
$itemUid = $wishlistItem['uid'];
112-
$query = $this->getQuery($wishlistUid, $itemUid);
111+
$itemId = $wishlistItem['id'];
112+
$query = $this->getQuery($wishlistId, $itemId);
113113
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
114114
}
115115

@@ -120,18 +120,17 @@ public function testAddItemsToCartWithoutId(): void
120120
*/
121121
public function testAddItemsToCartWithInvalidId(): void
122122
{
123-
$wishlistUid = '9999';
123+
$wishlistId = '9999';
124124

125125
$this->expectException(Exception::class);
126126
$this->expectExceptionMessage('The wishlist was not found.');
127127

128-
$wishlistUid = base64_encode((string) $wishlistUid);
129128
$wishlist = $this->getWishlist();
130129
$customerWishlist = $wishlist['customer']['wishlists'][0];
131130
$wishlistItem = $customerWishlist['items_v2']['items'][0];
132-
$itemUid = $wishlistItem['uid'];
131+
$itemId = $wishlistItem['id'];
133132

134-
$query = $this->getQuery($wishlistUid, $itemUid);
133+
$query = $this->getQuery($wishlistId, $itemId);
135134
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
136135
}
137136

@@ -155,20 +154,20 @@ private function getHeaderMap(string $username = 'customer@example.com', string
155154
/**
156155
* Returns GraphQl mutation string
157156
*
158-
* @param string $wishlistUid
157+
* @param string $wishlistId
159158
* @param string $itemId
160159
* @return string
161160
*/
162161
private function getQuery(
163-
string $wishlistUid,
162+
string $wishlistId,
164163
string $itemId
165164
): string {
166165
return <<<MUTATION
167166
mutation {
168167
addWishlistItemsToCart
169168
(
170-
wishlistUid: "{$wishlistUid}"
171-
wishlistItemUids: ["{$itemId}"]
169+
wishlistId: "{$wishlistId}"
170+
wishlistItemIds: ["{$itemId}"]
172171
) {
173172
status
174173
add_wishlist_items_to_cart_user_errors{
@@ -205,14 +204,12 @@ private function getCustomerWishlistQuery(): string
205204
customer {
206205
wishlists {
207206
id
208-
uid
209207
items_count
210208
sharing_code
211209
updated_at
212210
items_v2 {
213211
items {
214212
id
215-
uid
216213
quantity
217214
description
218215
product {

0 commit comments

Comments
 (0)