Skip to content

Commit d68f367

Browse files
30350:Unable to update the items of a bundle-product in a wishlist- Resolved webAPI test issues
1 parent 058f076 commit d68f367

File tree

3 files changed

+67
-38
lines changed

3 files changed

+67
-38
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function resolve(
104104
$wishlistOutput = "";
105105

106106
foreach ($wishlistItems as $wishlistItem) {
107-
$wishlistOutput = $this->updateWishlistItem->execute($wishlistItem->getId(), $wishlistItem, $wishlist);
107+
$wishlistOutput = $this->updateWishlistItem->execute($wishlistItem, $wishlist);
108108
}
109109

110110
if (count($wishlistOutput->getErrors()) !== count($wishlistItems)) {
@@ -174,3 +174,4 @@ private function getWishlist(?int $wishlistId, ?int $customerId): Wishlist
174174
return $wishlist;
175175
}
176176
}
177+

app/code/Magento/WishlistGraphQl/Model/UpdateWishlistItem.php

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Wishlist\Model\Wishlist;
1313
use Magento\Wishlist\Model\Wishlist\Data\WishlistOutput;
1414
use Magento\Wishlist\Model\Wishlist\BuyRequest\BuyRequestBuilder;
15+
use Magento\Wishlist\Model\Wishlist\Data\Error;
1516

1617
/**
1718
* Update wishlist items helper
@@ -34,6 +35,8 @@ class UpdateWishlistItem
3435
*/
3536
private $buyRequestBuilder;
3637

38+
private const ERROR_UNDEFINED = 'UNDEFINED';
39+
3740
/**
3841
* @param WishlistResourceModel $wishlistResource
3942
* @param BuyRequestBuilder $buyRequestBuilder
@@ -49,61 +52,84 @@ public function __construct(
4952
/**
5053
* Update wishlist Item and set data from request
5154
*
52-
* @param int $itemId
5355
* @param object $options
5456
* @param Wishlist $wishlist
5557
*
5658
* @return WishlistOutput
5759
* @throws GraphQlInputException
5860
* @throws \Magento\Framework\Exception\LocalizedException
5961
*/
60-
public function execute(int $itemId, object $options, Wishlist $wishlist)
62+
public function execute(object $options, Wishlist $wishlist)
6163
{
62-
$buyRequest = $this->buyRequestBuilder->build($options);
63-
$item = $wishlist->getItem((int)$itemId);
64-
65-
if (!$item) {
66-
throw new GraphQlInputException(__('We can\'t specify a wish list item.'));
67-
}
68-
69-
$product = $item->getProduct();
70-
$productId = $product->getId();
64+
$itemId = $options->getId();
65+
if ($wishlist->getItem($itemId) == null) {
66+
$this->addError(
67+
__(
68+
'The wishlist item with ID "%id" does not belong to the wishlist',
69+
['id' => $itemId]
70+
)->render()
71+
);
72+
} else {
73+
$buyRequest = $this->buyRequestBuilder->build($options);
74+
$item = $wishlist->getItem((int)$itemId);
75+
$product = $item->getProduct();
76+
$productId = $product->getId();
7177

72-
if ($productId) {
73-
$buyRequest->setData('action', 'updateItem');
74-
$product->setWishlistStoreId($item->getStoreId());
75-
$cartCandidates = $product->getTypeInstance()->processConfiguration($buyRequest, clone $product);
78+
if ($productId) {
79+
$buyRequest->setData('action', 'updateItem');
80+
$product->setWishlistStoreId($item->getStoreId());
81+
$cartCandidates = $product->getTypeInstance()->processConfiguration($buyRequest, clone $product);
7682

77-
/**
78-
* If the product with options existed or not
79-
*/
80-
if (is_string($cartCandidates)) {
81-
throw new GraphQlInputException(__('The product with options does not exist.'));
82-
}
83+
/**
84+
* If the product with options existed or not
85+
*/
86+
if (is_string($cartCandidates)) {
87+
throw new GraphQlInputException(__('The product with options does not exist.'));
88+
}
8389

84-
/**
85-
* If prepare process return one object
86-
*/
87-
if (!is_array($cartCandidates)) {
88-
$cartCandidates = [$cartCandidates];
89-
}
90+
/**
91+
* If prepare process return one object
92+
*/
93+
if (!is_array($cartCandidates)) {
94+
$cartCandidates = [$cartCandidates];
95+
}
9096

91-
foreach ($cartCandidates as $candidate) {
92-
if ($candidate->getParentProductId()) {
93-
continue;
97+
foreach ($cartCandidates as $candidate) {
98+
if ($candidate->getParentProductId()) {
99+
continue;
100+
}
101+
$candidate->setWishlistStoreId($item->getStoreId());
102+
$qty = $buyRequest->getData('qty') ? $buyRequest->getData('qty') : 1;
103+
$item->setOptions($candidate->getCustomOptions());
104+
$item->setQty($qty);
105+
if($options->getDescription()) {
106+
$item->setDescription($options->getDescription());
107+
}
94108
}
95-
$candidate->setWishlistStoreId($item->getStoreId());
96-
$qty = $buyRequest->getData('qty') ? $buyRequest->getData('qty') : 1;
97-
$item->setOptions($candidate->getCustomOptions());
98-
$item->setQty($qty);
109+
$this->wishlistResource->save($wishlist);
110+
} else {
111+
throw new GraphQlInputException(__('The product does not exist.'));
99112
}
100-
$this->wishlistResource->save($wishlist);
101-
} else {
102-
throw new GraphQlInputException(__('The product does not exist.'));
103113
}
104114
return $this->prepareOutput($wishlist);
105115
}
106116

117+
/**
118+
* Add wishlist line item error
119+
*
120+
* @param string $message
121+
* @param string|null $code
122+
*
123+
* @return void
124+
*/
125+
private function addError(string $message, string $code = null): void
126+
{
127+
$this->errors[] = new Error(
128+
$message,
129+
$code ?? self::ERROR_UNDEFINED
130+
);
131+
}
132+
107133
/**
108134
* Prepare output
109135
*
@@ -119,3 +145,4 @@ private function prepareOutput(Wishlist $wishlist): WishlistOutput
119145
return $output;
120146
}
121147
}
148+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function testUpdateBundleProductWithOptions(): void
8282

8383
$query = $this->getBundleQuery((int)$wishlistItemId, $qty, $bundleOptions, (int)$wishlistId);
8484
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
85+
8586
$this->assertArrayHasKey('updateProductsInWishlist', $response);
8687
$this->assertArrayHasKey('wishlist', $response['updateProductsInWishlist']);
8788
$response = $response['updateProductsInWishlist']['wishlist'];

0 commit comments

Comments
 (0)