Skip to content

Commit e263b0b

Browse files
committed
ACP2E-368: Issue when adding multiple items in addProductsToCart graphQL mutation
1 parent 6e5529f commit e263b0b

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private function prepareErrorOutput(Quote $cart, array $errors = []): AddProduct
246246
*/
247247
private function addQuoteItem(Quote $quote, Item $quoteItem): void
248248
{
249-
if ($quoteItem->getOriginalItem() && $quoteItem->getOriginalItem()->getId()) {
249+
if ($quoteItem->getOriginalItem()) {
250250
$quote->deleteItem($quoteItem->getOriginalItem());
251251
$quoteItem->unsOriginalItem();
252252
}

app/code/Magento/Quote/Model/Quote/Item/Compare.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ protected function getOptionValues($value)
6868
*/
6969
public function compare(Item $target, Item $compared)
7070
{
71+
if ($target === $compared) {
72+
return true;
73+
}
74+
7175
if ($target->getProductId() != $compared->getProductId()) {
7276
return false;
7377
}

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

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Magento\GraphQl\Quote;
99

10+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
11+
use Magento\TestFramework\Fixture\DataFixtureStorage;
12+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
1013
use Magento\TestFramework\Helper\Bootstrap;
1114
use Magento\TestFramework\TestCase\GraphQlAbstract;
1215

@@ -30,6 +33,16 @@ class AddSimpleProductToCartSingleMutationTest extends GraphQlAbstract
3033
*/
3134
private $getCartItemOptionsFromUID;
3235

36+
/**
37+
* @var QuoteIdToMaskedQuoteIdInterface
38+
*/
39+
private $quoteIdToMaskedQuoteIdInterface;
40+
41+
/**
42+
* @var DataFixtureStorage
43+
*/
44+
private $fixtures;
45+
3346
/**
3447
* @inheritdoc
3548
*/
@@ -41,6 +54,8 @@ protected function setUp(): void
4154
$this->getCustomOptionsWithIDV2ForQueryBySku = $objectManager->get(
4255
GetCustomOptionsWithUIDForQueryBySku::class
4356
);
57+
$this->quoteIdToMaskedQuoteIdInterface = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
58+
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();
4459
}
4560

4661
/**
@@ -81,7 +96,7 @@ public function testAddSimpleProductWithOptions()
8196
$customizableOptionsOutput =
8297
$response['addProductsToCart']['cart']['items'][0]['customizable_options'];
8398

84-
foreach ($customizableOptionsOutput as $key => $customizableOptionOutput) {
99+
foreach ($customizableOptionsOutput as $customizableOptionOutput) {
85100
$customizableOptionOutputValues = [];
86101
foreach ($customizableOptionOutput['values'] as $customizableOptionOutputValue) {
87102
$customizableOptionOutputValues[] = $customizableOptionOutputValue['value'];
@@ -220,6 +235,51 @@ public function testAddProductNotAssignedToWebsite(string $reservedOrderId, stri
220235
self::assertEquals('PRODUCT_NOT_FOUND', $response['addProductsToCart']['user_errors'][0]['code']);
221236
}
222237

238+
/**
239+
* @magentoApiDataFixture Magento\Catalog\Test\Fixture\Product as:product1
240+
* @magentoApiDataFixture Magento\Catalog\Test\Fixture\Product as:product2
241+
* @magentoApiDataFixture Magento\Quote\Test\Fixture\GuestCart as:cart
242+
*/
243+
public function testAddMultipleProductsToEmptyCart(): void
244+
{
245+
$product1 = $this->fixtures->get('product1');
246+
$product2 = $this->fixtures->get('product2');
247+
$cart = $this->fixtures->get('cart');
248+
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
249+
$query = $this->getAddMultipleProductsToCartAndReturnCartTotalsMutation(
250+
$maskedQuoteId,
251+
[
252+
[
253+
'sku' => $product1->getSku(),
254+
'quantity' => 2
255+
],
256+
[
257+
'sku' => $product2->getSku(),
258+
'quantity' => 3
259+
]
260+
]
261+
);
262+
$response = $this->graphQlMutation($query);
263+
$result = $response['addProductsToCart'];
264+
self::assertEmpty($result['user_errors']);
265+
self::assertNotEmpty($result['cart']['items']);
266+
267+
$cartItem = $result['cart']['items'][0];
268+
self::assertEquals($product1->getSku(), $cartItem['product']['sku']);
269+
self::assertEquals(2, $cartItem['quantity']);
270+
self::assertEquals(10, $cartItem['prices']['price']['value']);
271+
self::assertEquals(20, $cartItem['prices']['row_total']['value']);
272+
273+
$cartItem = $result['cart']['items'][1];
274+
self::assertEquals($product2->getSku(), $cartItem['product']['sku']);
275+
self::assertEquals(3, $cartItem['quantity']);
276+
self::assertEquals(10, $cartItem['prices']['price']['value']);
277+
self::assertEquals(30, $cartItem['prices']['row_total']['value']);
278+
279+
$cartTotals = $result['cart']['prices'];
280+
self::assertEquals(50, $cartTotals['grand_total']['value']);
281+
}
282+
223283
/**
224284
* @return array
225285
*/
@@ -317,6 +377,61 @@ private function getAddToCartMutation(
317377
}
318378
}
319379
}
380+
MUTATION;
381+
}
382+
383+
/**
384+
* Returns GraphQl mutation for addProductsToCart with cart totals
385+
*
386+
* @param string $maskedQuoteId
387+
* @param array $cartItems
388+
* @return string
389+
*/
390+
private function getAddMultipleProductsToCartAndReturnCartTotalsMutation(
391+
string $maskedQuoteId,
392+
array $cartItems
393+
): string {
394+
$cartItemsQuery = preg_replace(
395+
'/"([^"]+)"\s*:\s*/',
396+
'$1:',
397+
json_encode($cartItems)
398+
);
399+
return <<<MUTATION
400+
mutation {
401+
addProductsToCart(
402+
cartId: "{$maskedQuoteId}",
403+
cartItems: $cartItemsQuery
404+
) {
405+
cart {
406+
items {
407+
product {
408+
sku
409+
}
410+
quantity
411+
prices {
412+
price {
413+
value
414+
currency
415+
}
416+
row_total {
417+
value
418+
currency
419+
}
420+
}
421+
}
422+
prices {
423+
grand_total {
424+
value
425+
currency
426+
}
427+
}
428+
},
429+
user_errors {
430+
code
431+
message
432+
}
433+
}
434+
}
320435
MUTATION;
321436
}
322437
}

0 commit comments

Comments
 (0)