Skip to content

Commit 81d4a0d

Browse files
ENGCOM-4871: Cannot return null for non-nullable field SelectedCustomizableOptionValue.sort_order and Call to a member function getPriceType() on n #525
- Merge Pull Request magento/graphql-ce#525 from XxXgeoXxX/graphql-ce:2.3-develop#474 - Merged commits: 1. 81daba8 2. 742e6a7 3. 30bc7b7 4. 733ddf9 5. 1315577 6. b115f1c 7. 05ea6ef
2 parents 93a8162 + 05ea6ef commit 81d4a0d

File tree

5 files changed

+100
-125
lines changed

5 files changed

+100
-125
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,13 @@
1313
use Magento\Framework\Exception\NoSuchEntityException;
1414
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1515
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
16-
use Magento\Framework\Stdlib\ArrayManager;
1716
use Magento\Quote\Model\Quote;
1817

1918
/**
2019
* Add simple product to cart
21-
*
22-
* TODO: should be replaced for different types resolver
2320
*/
2421
class AddSimpleProductToCart
2522
{
26-
/**
27-
* @var ArrayManager
28-
*/
29-
private $arrayManager;
30-
3123
/**
3224
* @var DataObjectFactory
3325
*/
@@ -39,16 +31,13 @@ class AddSimpleProductToCart
3931
private $productRepository;
4032

4133
/**
42-
* @param ArrayManager $arrayManager
4334
* @param DataObjectFactory $dataObjectFactory
4435
* @param ProductRepositoryInterface $productRepository
4536
*/
4637
public function __construct(
47-
ArrayManager $arrayManager,
4838
DataObjectFactory $dataObjectFactory,
4939
ProductRepositoryInterface $productRepository
5040
) {
51-
$this->arrayManager = $arrayManager;
5241
$this->dataObjectFactory = $dataObjectFactory;
5342
$this->productRepository = $productRepository;
5443
}
@@ -67,11 +56,6 @@ public function execute(Quote $cart, array $cartItemData): void
6756
{
6857
$sku = $this->extractSku($cartItemData);
6958
$quantity = $this->extractQuantity($cartItemData);
70-
if ($quantity <= 0) {
71-
throw new GraphQlInputException(
72-
__('Please enter a number greater than 0 in this field.')
73-
);
74-
}
7559
$customizableOptions = $this->extractCustomizableOptions($cartItemData);
7660

7761
try {
@@ -105,11 +89,10 @@ public function execute(Quote $cart, array $cartItemData): void
10589
*/
10690
private function extractSku(array $cartItemData): string
10791
{
108-
$sku = $this->arrayManager->get('data/sku', $cartItemData);
109-
if (!isset($sku)) {
110-
throw new GraphQlInputException(__('Missing key "sku" in cart item data'));
92+
if (!isset($cartItemData['data']['sku']) || empty($cartItemData['data']['sku'])) {
93+
throw new GraphQlInputException(__('Missed "sku" in cart item data'));
11194
}
112-
return (string)$sku;
95+
return (string)$cartItemData['data']['sku'];
11396
}
11497

11598
/**
@@ -121,11 +104,17 @@ private function extractSku(array $cartItemData): string
121104
*/
122105
private function extractQuantity(array $cartItemData): float
123106
{
124-
$quantity = $this->arrayManager->get('data/quantity', $cartItemData);
125-
if (!isset($quantity)) {
126-
throw new GraphQlInputException(__('Missing key "quantity" in cart item data'));
107+
if (!isset($cartItemData['data']['quantity'])) {
108+
throw new GraphQlInputException(__('Missed "qty" in cart item data'));
109+
}
110+
$quantity = (float)$cartItemData['data']['quantity'];
111+
112+
if ($quantity <= 0) {
113+
throw new GraphQlInputException(
114+
__('Please enter a number greater than 0 in this field.')
115+
);
127116
}
128-
return (float)$quantity;
117+
return $quantity;
129118
}
130119

131120
/**
@@ -136,11 +125,17 @@ private function extractQuantity(array $cartItemData): float
136125
*/
137126
private function extractCustomizableOptions(array $cartItemData): array
138127
{
139-
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
128+
if (!isset($cartItemData['customizable_options']) || empty($cartItemData['customizable_options'])) {
129+
return [];
130+
}
140131

141132
$customizableOptionsData = [];
142-
foreach ($customizableOptions as $customizableOption) {
143-
$customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
133+
foreach ($cartItemData['customizable_options'] as $customizableOption) {
134+
if (isset($customizableOption['value_string'])) {
135+
$customizableOptionsData[$customizableOption['id']] = $this->convertCustomOptionValue(
136+
$customizableOption['value_string']
137+
);
138+
}
144139
}
145140
return $customizableOptionsData;
146141
}
@@ -161,4 +156,20 @@ private function createBuyRequest(float $quantity, array $customOptions): DataOb
161156
],
162157
]);
163158
}
159+
160+
/**
161+
* Convert custom options vakue
162+
*
163+
* @param string $value
164+
* @return string|array
165+
*/
166+
private function convertCustomOptionValue(string $value)
167+
{
168+
$value = trim($value);
169+
if (substr($value, 0, 1) === "[" &&
170+
substr($value, strlen($value) - 1, 1) === "]") {
171+
return explode(',', substr($value, 1, -1));
172+
}
173+
return $value;
174+
}
164175
}

app/code/Magento/QuoteGraphQl/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<item name="area" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Text</item>
2727
<item name="drop_down" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown</item>
2828
<item name="radio" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown</item>
29-
<item name="checkbox" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown</item>
29+
<item name="checkbox" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple</item>
3030
<item name="multiple" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple</item>
3131
</argument>
3232
</arguments>

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ input CartItemInput {
5252

5353
input CustomizableOptionInput {
5454
id: Int!
55-
value: String!
55+
value_string: String!
5656
}
5757

5858
input ApplyCouponToCartInput {
@@ -314,18 +314,16 @@ interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\
314314
type SelectedCustomizableOption {
315315
id: Int!
316316
label: String!
317-
type: String!
318-
is_required: Int!
317+
is_required: Boolean!
319318
values: [SelectedCustomizableOptionValue!]!
320319
sort_order: Int!
321320
}
322321

323322
type SelectedCustomizableOptionValue {
324323
id: Int!
325324
label: String!
326-
value: String!
325+
value: String
327326
price: CartItemSelectedOptionValuePrice!
328-
sort_order: Int!
329327
}
330328

331329
type CartItemSelectedOptionValuePrice {

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

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,11 @@ public function testAddSimpleProductWithOptions()
5050
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
5151

5252
$customOptionsValues = $this->getCustomOptionsValuesForQuery($sku);
53-
5453
/* Generate customizable options fragment for GraphQl request */
55-
$queryCustomizableOptions = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
54+
$queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
5655

57-
$query = <<<QUERY
58-
mutation {
59-
addSimpleProductsToCart(
60-
input: {
61-
cart_id: "{$maskedQuoteId}",
62-
cartItems: [
63-
{
64-
data: {
65-
quantity: $quantity
66-
sku: "$sku"
67-
},
68-
customizable_options: $queryCustomizableOptions
69-
}
70-
]
71-
}
72-
) {
73-
cart {
74-
items {
75-
... on SimpleCartItem {
76-
customizable_options {
77-
label
78-
values {
79-
value
80-
}
81-
}
82-
}
83-
}
84-
}
85-
}
86-
}
87-
QUERY;
56+
$customizableOptions = "customizable_options: {$queryCustomizableOptionValues}";
57+
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
8858

8959
$response = $this->graphQlMutation($query);
9060

@@ -95,7 +65,7 @@ public function testAddSimpleProductWithOptions()
9565
$assignedOptionsCount = count($customOptionsValues);
9666
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
9767
self::assertEquals(
98-
$customOptionsValues[$counter]['value'],
68+
$customOptionsValues[$counter]['value_string'],
9969
$customizableOptionsOutput[$counter]['values'][0]['value']
10070
);
10171
}
@@ -107,13 +77,31 @@ public function testAddSimpleProductWithOptions()
10777
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
10878
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
10979
*/
110-
public function testAddSimpleProductWithNoRequiredOptionsSet()
80+
public function testAddSimpleProductWithMissedRequiredOptionsSet()
11181
{
82+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
11283
$sku = 'simple';
11384
$quantity = 1;
114-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
85+
$customizableOptions = '';
11586

116-
$query = <<<QUERY
87+
$query = $this->getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
88+
89+
self::expectExceptionMessage(
90+
'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.'
91+
);
92+
$this->graphQlMutation($query);
93+
}
94+
95+
/**
96+
* @param string $maskedQuoteId
97+
* @param string $sku
98+
* @param float $quantity
99+
* @param string $customizableOptions
100+
* @return string
101+
*/
102+
private function getQuery(string $maskedQuoteId, string $sku, float $quantity, string $customizableOptions): string
103+
{
104+
return <<<QUERY
117105
mutation {
118106
addSimpleProductsToCart(
119107
input: {
@@ -124,6 +112,7 @@ public function testAddSimpleProductWithNoRequiredOptionsSet()
124112
quantity: $quantity
125113
sku: "$sku"
126114
}
115+
{$customizableOptions}
127116
}
128117
]
129118
}
@@ -134,7 +123,7 @@ public function testAddSimpleProductWithNoRequiredOptionsSet()
134123
customizable_options {
135124
label
136125
values {
137-
value
126+
value
138127
}
139128
}
140129
}
@@ -143,12 +132,6 @@ public function testAddSimpleProductWithNoRequiredOptionsSet()
143132
}
144133
}
145134
QUERY;
146-
147-
self::expectExceptionMessage(
148-
'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.'
149-
);
150-
151-
$this->graphQlMutation($query);
152135
}
153136

154137
/**
@@ -168,13 +151,13 @@ private function getCustomOptionsValuesForQuery(string $sku): array
168151
if ($optionType == 'field' || $optionType == 'area') {
169152
$customOptionsValues[] = [
170153
'id' => (int) $customOption->getOptionId(),
171-
'value' => 'test'
154+
'value_string' => 'test'
172155
];
173156
} elseif ($optionType == 'drop_down') {
174157
$optionSelectValues = $customOption->getValues();
175158
$customOptionsValues[] = [
176159
'id' => (int) $customOption->getOptionId(),
177-
'value' => reset($optionSelectValues)->getOptionTypeId()
160+
'value_string' => reset($optionSelectValues)->getOptionTypeId()
178161
];
179162
}
180163
}

0 commit comments

Comments
 (0)