Skip to content

Commit 6ad376c

Browse files
committed
Merge branch 'ACP2E-2897' of https://github.com/adobe-commerce-tier-4/magento2ce into T4-PR-03-18-2024
2 parents 9239bb3 + cc94c5b commit 6ad376c

File tree

2 files changed

+199
-2
lines changed

2 files changed

+199
-2
lines changed

app/code/Magento/Quote/Model/Cart/AddProductsToCart.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
157157
$cartItemPosition
158158
);
159159
} else {
160-
$product = $this->productReader->getProductBySku($sku);
160+
$productBySku = $this->productReader->getProductBySku($sku);
161+
$product = isset($productBySku) ? clone $productBySku : null;
161162
if (!$product || !$product->isSaleable() || !$product->isAvailable()) {
162163
$errors[] = $this->error->create(
163164
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddProductWithOptionsToCartTest.php

Lines changed: 197 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,64 @@ public function testAddProductWithOptionsResponse()
105105
);
106106
}
107107

108+
#[
109+
DataFixture(GuestCart::class, as: 'quote'),
110+
DataFixture(QuoteIdMask::class, ['cart_id' => '$quote.id$'], 'quoteIdMask'),
111+
DataFixture(
112+
Product::class,
113+
[
114+
'sku' => 'simple1',
115+
'options' => [
116+
[
117+
'title' => 'option1',
118+
'type' => ProductCustomOptionInterface::OPTION_TYPE_FIELD,
119+
]
120+
]
121+
],
122+
'product1'
123+
),
124+
DataFixture(Indexer::class, as: 'indexer')
125+
]
126+
public function testAddSameProductWithDifferentOptionValues()
127+
{
128+
$uidEncoder = Bootstrap::getObjectManager()->create(Uid::class);
129+
130+
$cartId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
131+
$product = DataFixtureStorageManager::getStorage()->get('product1');
132+
/* @var \Magento\Catalog\Api\Data\ProductInterface $product */
133+
$sku = $product->getSku();
134+
$option = $product->getOptions();
135+
$optionUid = $uidEncoder->encode(
136+
'custom-option' . '/' . $option[0]->getData()['option_id']
137+
);
138+
139+
// Assert if product options for item added to the cart
140+
// are present in mutation response after product with selected option was added
141+
$mutation = $this->getAddProductWithDifferentOptionValuesMutation(
142+
$cartId,
143+
$sku,
144+
$optionUid
145+
);
146+
$response = $this->graphQlMutation($mutation);
147+
148+
$this->assertArrayHasKey('items', $response['addProductsToCart']['cart']);
149+
$this->assertCount(2, $response['addProductsToCart']['cart']['items']);
150+
$this->assertArrayHasKey('customizable_options', $response['addProductsToCart']['cart']['items'][0]);
151+
152+
$this->assertEquals(
153+
$response['addProductsToCart']['cart']['items'],
154+
$this->getExpectedResponseForDifferentOptionValues($optionUid, $sku)
155+
);
156+
157+
// Assert if product options for item in the cart are present in the response
158+
$query = $this->getCartQueryForDifferentOptionValues($cartId);
159+
$response = $this->graphQlQuery($query);
160+
$this->assertEquals(
161+
$this->getExpectedResponseForDifferentOptionValues($optionUid, $sku),
162+
$response['cart']['items']
163+
);
164+
}
165+
108166
#[
109167
DataFixture(GuestCart::class, as: 'quote'),
110168
DataFixture(QuoteIdMask::class, ['cart_id' => '$quote.id$'], 'quoteIdMask'),
@@ -268,7 +326,67 @@ private function getAddProductWithOptionMutation(string $cartId, string $sku, st
268326
}
269327
}
270328
}
271-
}
329+
}
330+
QRY;
331+
}
332+
333+
/**
334+
* Returns mutation for the test with different option values
335+
*
336+
* @param string $cartId
337+
* @param string $sku
338+
* @param string $optionUid
339+
* @return string
340+
*/
341+
private function getAddProductWithDifferentOptionValuesMutation(
342+
string $cartId,
343+
string $sku,
344+
string $optionUid
345+
): string {
346+
return <<<QRY
347+
mutation {
348+
addProductsToCart(
349+
cartId: "{$cartId}"
350+
cartItems: [
351+
{
352+
quantity:1
353+
sku: "{$sku}"
354+
entered_options: [{
355+
uid:"{$optionUid}",
356+
value:"value1"
357+
}]
358+
}
359+
{
360+
quantity:1
361+
sku: "{$sku}"
362+
entered_options: [{
363+
uid:"{$optionUid}",
364+
value:"value2"
365+
}]
366+
}
367+
]
368+
) {
369+
cart {
370+
id
371+
items {
372+
quantity
373+
product {
374+
sku
375+
}
376+
... on SimpleCartItem {
377+
customizable_options {
378+
customizable_option_uid
379+
label
380+
values {
381+
customizable_option_value_uid
382+
value
383+
}
384+
}
385+
}
386+
}
387+
}
388+
}
389+
}
272390
QRY;
273391
}
274392

@@ -307,6 +425,39 @@ private function getCartQuery(string $cartId): string
307425
QRY;
308426
}
309427

428+
/**
429+
* Returns query to get cart with information about item and associated product with different option values
430+
*
431+
* @param string $cartId
432+
* @return string
433+
*/
434+
private function getCartQueryForDifferentOptionValues(string $cartId): string
435+
{
436+
return <<<QRY
437+
query {
438+
cart(cart_id: "{$cartId}")
439+
{
440+
items {
441+
quantity
442+
product {
443+
sku
444+
}
445+
... on SimpleCartItem {
446+
customizable_options {
447+
customizable_option_uid
448+
label
449+
values {
450+
customizable_option_value_uid
451+
value
452+
}
453+
}
454+
}
455+
}
456+
}
457+
}
458+
QRY;
459+
}
460+
310461
/**
311462
* Formats and returns expected response
312463
*
@@ -406,4 +557,49 @@ private function getExpectedResponse(string $selectedOptionUid, array $productOp
406557
]
407558
];
408559
}
560+
561+
/**
562+
* Returns formatted response for test with different option values
563+
*
564+
* @param string $selectedOptionUid
565+
* @param array $productOptions
566+
* @return array
567+
*/
568+
private function getExpectedResponseForDifferentOptionValues(string $optionUid, string $sku): array
569+
{
570+
return [
571+
0 => [
572+
"quantity" => 1,
573+
"product" => ["sku" => "{$sku}"],
574+
"customizable_options" => [
575+
0 => [
576+
"customizable_option_uid" => "{$optionUid}",
577+
"label" => "option1",
578+
"values" => [
579+
0 => [
580+
"customizable_option_value_uid" => "{$optionUid}",
581+
"value" => "value1"
582+
]
583+
]
584+
]
585+
]
586+
],
587+
1 => [
588+
"quantity" => 1,
589+
"product" => ["sku" => "{$sku}"],
590+
"customizable_options" => [
591+
0 => [
592+
"customizable_option_uid" => "{$optionUid}",
593+
"label" => "option1",
594+
"values" => [
595+
0 => [
596+
"customizable_option_value_uid" => "{$optionUid}",
597+
"value" => "value2"
598+
]
599+
]
600+
]
601+
]
602+
],
603+
];
604+
}
409605
}

0 commit comments

Comments
 (0)