Skip to content

Commit e143f82

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'origin/MC-36898' into 2.4-develop
2 parents 71ed28e + b853f92 commit e143f82

File tree

2 files changed

+189
-3
lines changed

2 files changed

+189
-3
lines changed

app/code/Magento/Wishlist/Model/Wishlist/BuyRequest/BundleDataProvider.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Wishlist\Model\Wishlist\BuyRequest;
99

10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Wishlist\Model\Wishlist\Data\WishlistItem;
1112

1213
/**
@@ -32,15 +33,48 @@ public function execute(WishlistItem $wishlistItem, ?int $productId): array
3233
continue;
3334
}
3435

35-
[, $optionId, $optionValueId, $optionQuantity] = $optionData;
36+
[$optionType, $optionId, $optionValueId, $optionQuantity] = $optionData;
3637

37-
$bundleOptionsData['bundle_option'][$optionId] = $optionValueId;
38-
$bundleOptionsData['bundle_option_qty'][$optionId] = $optionQuantity;
38+
if ($optionType == self::PROVIDER_OPTION_TYPE) {
39+
$bundleOptionsData['bundle_option'][$optionId] = $optionValueId;
40+
$bundleOptionsData['bundle_option_qty'][$optionId] = $optionQuantity;
41+
}
42+
}
43+
//for bundle options with custom quantity
44+
foreach ($wishlistItem->getEnteredOptions() as $option) {
45+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
46+
$optionData = \explode('/', base64_decode($option->getUid()));
47+
48+
if ($this->isProviderApplicable($optionData) === false) {
49+
continue;
50+
}
51+
$this->validateInput($optionData);
52+
53+
[$optionType, $optionId, $optionValueId] = $optionData;
54+
if ($optionType == self::PROVIDER_OPTION_TYPE) {
55+
$optionQuantity = $option->getValue();
56+
$bundleOptionsData['bundle_option'][$optionId] = $optionValueId;
57+
$bundleOptionsData['bundle_option_qty'][$optionId] = $optionQuantity;
58+
}
3959
}
4060

4161
return $bundleOptionsData;
4262
}
4363

64+
/**
65+
* Validates the provided options structure
66+
*
67+
* @param array $optionData
68+
* @throws LocalizedException
69+
*/
70+
private function validateInput(array $optionData): void
71+
{
72+
if (count($optionData) !== 4) {
73+
$errorMessage = __('Wrong format of the entered option data');
74+
throw new LocalizedException($errorMessage);
75+
}
76+
}
77+
4478
/**
4579
* Checks whether this provider is applicable for the current option
4680
*

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

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,46 @@ public function testAddBundleProductWithOptions(): void
9898
$this->assertEquals(Select::NAME, $bundleOptions[0]['type']);
9999
}
100100

101+
/**
102+
* @magentoApiDataFixture Magento/Bundle/_files/product_with_multiple_options_and_custom_quantity.php
103+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
104+
*
105+
* @throws Exception
106+
*/
107+
public function testAddingBundleItemWithCustomOptionQuantity()
108+
{
109+
$response = $this->graphQlQuery($this->getProductQuery("bundle-product"));
110+
$bundleItem = $response['products']['items'][0];
111+
$sku = $bundleItem['sku'];
112+
$bundleOptions = $bundleItem['items'];
113+
$customerId = 1;
114+
$uId0 = $bundleOptions[0]['options'][0]['uid'];
115+
$uId1 = $bundleOptions[1]['options'][0]['uid'];
116+
$query= $this->getQueryWithCustomOptionQuantity($sku, 5, $uId0, $uId1);
117+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
118+
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId, true);
119+
/** @var Item $item */
120+
$item = $wishlist->getItemCollection()->getFirstItem();
121+
122+
$this->assertArrayHasKey('addProductsToWishlist', $response);
123+
$this->assertArrayHasKey('wishlist', $response['addProductsToWishlist']);
124+
$response = $response['addProductsToWishlist']['wishlist'];
125+
$this->assertEquals($wishlist->getItemsCount(), $response['items_count']);
126+
$this->assertEquals($wishlist->getSharingCode(), $response['sharing_code']);
127+
$this->assertEquals($wishlist->getUpdatedAt(), $response['updated_at']);
128+
$this->assertEquals($item->getData('qty'), $response['items_v2'][0]['quantity']);
129+
$this->assertEquals($item->getDescription(), $response['items_v2'][0]['description']);
130+
$this->assertEquals($item->getAddedAt(), $response['items_v2'][0]['added_at']);
131+
$this->assertNotEmpty($response['items_v2'][0]['bundle_options']);
132+
$bundleOptions = $response['items_v2'][0]['bundle_options'];
133+
$this->assertEquals('Option 1', $bundleOptions[0]['label']);
134+
$bundleOptionOneValues = $bundleOptions[0]['values'];
135+
$this->assertEquals(7, $bundleOptionOneValues[0]['quantity']);
136+
$this->assertEquals('Option 2', $bundleOptions[1]['label']);
137+
$bundleOptionTwoValues = $bundleOptions[1]['values'];
138+
$this->assertEquals(1, $bundleOptionTwoValues[0]['quantity']);
139+
}
140+
101141
/**
102142
* Authentication header map
103143
*
@@ -179,6 +219,118 @@ private function getQuery(
179219
MUTATION;
180220
}
181221

222+
/**
223+
* Query with custom option quantity
224+
*
225+
* @param string $sku
226+
* @param int $qty
227+
* @param string $uId0
228+
* @param string $uId1
229+
* @param int $wishlistId
230+
* @return string
231+
*/
232+
private function getQueryWithCustomOptionQuantity(
233+
string $sku,
234+
int $qty,
235+
string $uId0,
236+
string $uId1,
237+
int $wishlistId = 0
238+
): string {
239+
return <<<MUTATION
240+
mutation {
241+
addProductsToWishlist(
242+
wishlistId: {$wishlistId},
243+
wishlistItems: [
244+
{
245+
sku: "{$sku}"
246+
quantity: {$qty}
247+
entered_options: [
248+
{
249+
uid:"{$uId0}",
250+
value:"7"
251+
},
252+
{
253+
uid:"{$uId1}",
254+
value:"7"
255+
}
256+
]
257+
}
258+
]
259+
) {
260+
user_errors {
261+
code
262+
message
263+
}
264+
wishlist {
265+
id
266+
sharing_code
267+
items_count
268+
updated_at
269+
items_v2 {
270+
id
271+
description
272+
quantity
273+
added_at
274+
... on BundleWishlistItem {
275+
bundle_options {
276+
id
277+
label
278+
type
279+
values {
280+
id
281+
label
282+
quantity
283+
price
284+
}
285+
}
286+
}
287+
}
288+
}
289+
}
290+
}
291+
MUTATION;
292+
}
293+
294+
/**
295+
* Returns GraphQL query for retrieving a product with customizable options
296+
*
297+
* @param string $sku
298+
* @return string
299+
*/
300+
private function getProductQuery(string $sku): string
301+
{
302+
return <<<QUERY
303+
{
304+
products(search: "{$sku}") {
305+
items {
306+
sku
307+
... on BundleProduct {
308+
items {
309+
sku
310+
option_id
311+
required
312+
type
313+
title
314+
options {
315+
uid
316+
label
317+
product {
318+
sku
319+
}
320+
can_change_quantity
321+
id
322+
price
323+
324+
quantity
325+
}
326+
}
327+
}
328+
}
329+
}
330+
}
331+
QUERY;
332+
}
333+
182334
/**
183335
* @param int $optionId
184336
* @param int $selectionId

0 commit comments

Comments
 (0)