Skip to content

Commit d845f76

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-1975' into JUN062023_PR_sarmistha
2 parents 8fa511c + 7667122 commit d845f76

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function loadProducts(array $skus, int $storeId): void
6262
$this->productCollection->addFieldToFilter(ProductInterface::SKU, ['in' => $skus]);
6363
$this->productCollection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
6464
$this->productCollection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
65+
$this->productCollection->addOptionsToResult();
6566
$this->productCollection->load();
6667
foreach ($this->productCollection->getItems() as $productItem) {
6768
$this->productsBySku[$productItem->getData(ProductInterface::SKU)] = $productItem;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
13+
/**
14+
* Test GraphQL can add product with customizable option to cart
15+
*/
16+
class AddProductWithCustomizableOptionToCartTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var GetCustomOptionsWithUIDForQueryBySku
20+
*/
21+
private $getCustomOptionsWithIDV2ForQueryBySku;
22+
23+
/**
24+
* @var GetMaskedQuoteIdByReservedOrderId
25+
*/
26+
private $getMaskedQuoteIdByReservedOrderId;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function setUp(): void
32+
{
33+
$objectManager = Bootstrap::getObjectManager();
34+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
35+
$this->getCustomOptionsWithIDV2ForQueryBySku = $objectManager->get(
36+
GetCustomOptionsWithUIDForQueryBySku::class
37+
);
38+
}
39+
40+
/**
41+
* Test adding a simple product to the shopping cart with all supported
42+
* customizable options assigned
43+
*
44+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
45+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
46+
*/
47+
public function testAddSimpleProductWithOptions()
48+
{
49+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
50+
51+
$sku = 'simple';
52+
$qty = 1;
53+
54+
$itemOptions = $this->getCustomOptionsWithIDV2ForQueryBySku->execute($sku);
55+
56+
/* The type field is only required for assertions, it should not be present in query */
57+
foreach ($itemOptions['entered_options'] as &$enteredOption) {
58+
if (isset($enteredOption['type'])) {
59+
unset($enteredOption['type']);
60+
}
61+
}
62+
63+
$productOptionsQuery = preg_replace(
64+
'/"([^"]+)"\s*:\s*/',
65+
'$1:',
66+
json_encode($itemOptions)
67+
);
68+
69+
$query = $this->getAddToCartMutation($maskedQuoteId, $qty, $sku, trim($productOptionsQuery, '{}'));
70+
$response = $this->graphQlMutation($query);
71+
self::assertArrayHasKey('items', $response['addProductsToCart']['cart']);
72+
self::assertCount($qty, $response['addProductsToCart']['cart']['items']);
73+
self::assertNotEmpty($response['addProductsToCart']['cart']['items'][0]['customizable_options']);
74+
}
75+
76+
/**
77+
* Returns GraphQl query string
78+
*
79+
* @param string $maskedQuoteId
80+
* @param int $qty
81+
* @param string $sku
82+
* @param string $customizableOptions
83+
* @return string
84+
*/
85+
private function getAddToCartMutation(
86+
string $maskedQuoteId,
87+
int $qty,
88+
string $sku,
89+
string $customizableOptions = ''
90+
): string {
91+
return <<<MUTATION
92+
mutation {
93+
addProductsToCart(
94+
cartId: "{$maskedQuoteId}",
95+
cartItems: [
96+
{
97+
sku: "{$sku}"
98+
quantity: {$qty}
99+
{$customizableOptions}
100+
}
101+
]
102+
) {
103+
cart {
104+
items {
105+
product {
106+
name
107+
sku
108+
}
109+
... on SimpleCartItem {
110+
customizable_options {
111+
label
112+
customizable_option_uid
113+
values {
114+
value
115+
customizable_option_value_uid
116+
}
117+
}
118+
}
119+
}
120+
}
121+
user_errors {
122+
code
123+
message
124+
}
125+
}
126+
}
127+
MUTATION;
128+
}
129+
}

0 commit comments

Comments
 (0)