Skip to content

Commit b2af5db

Browse files
committed
MC-36276: Internal Server error when adding a configurable product with customized option to cart
1 parent e23e810 commit b2af5db

File tree

5 files changed

+188
-1
lines changed

5 files changed

+188
-1
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/ConfigurableCartItemOptions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5555

5656
$result = [];
5757
foreach ($this->configurationHelper->getOptions($cartItem) as $option) {
58+
if (isset($option['option_type'])) {
59+
//Don't return customizable options in this resolver
60+
continue;
61+
}
5862
$result[] = [
5963
'id' => $option['option_id'],
6064
'option_label' => $option['label'],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ input ConfigurableProductCartItemInput {
5858
}
5959

6060
type ConfigurableCartItem implements CartItemInterface {
61-
customizable_options: [SelectedCustomizableOption]!
61+
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
6262
configurable_options: [SelectedConfigurableOption!]! @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ConfigurableCartItemOptions")
6363
}
6464

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/AddConfigurableProductToCartTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,94 @@ public function testOutOfStockVariationToCart()
328328
$this->graphQlMutation($query);
329329
}
330330

331+
/**
332+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_with_custom_option_dropdown.php
333+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
334+
*/
335+
public function testAddConfigurableProductToCartWithCustomOption()
336+
{
337+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
338+
$sku = 'configurable';
339+
$variantSku = 'simple_10';
340+
$productOptions = $this->getAvailableProductCustomOption($sku);
341+
$optionId = $productOptions[0]['option_id'];
342+
$optionValueId = $productOptions[0]['value'][1]['option_type_id'];
343+
344+
$mutation = <<<QUERY
345+
mutation {
346+
addConfigurableProductsToCart(input: {
347+
cart_id: "{$maskedQuoteId}",
348+
cart_items: [
349+
{
350+
parent_sku: "{$sku}",
351+
variant_sku: "{$variantSku}",
352+
data: {
353+
sku: "{$variantSku}",
354+
quantity: 1
355+
},
356+
customizable_options: [
357+
{id: {$optionId}, value_string: "{$optionValueId}"}]
358+
}
359+
]
360+
}) {
361+
cart {
362+
items {
363+
id
364+
quantity
365+
product {
366+
sku
367+
name
368+
}
369+
... on ConfigurableCartItem {
370+
configurable_options {
371+
option_label
372+
value_label
373+
}
374+
customizable_options {
375+
id
376+
label
377+
values{
378+
label
379+
value
380+
}
381+
}
382+
}
383+
}
384+
}
385+
}
386+
}
387+
QUERY;
388+
389+
$response = $this->graphQlMutation($mutation);
390+
$this->assertArrayNotHasKey('errors', $response);
391+
$this->assertCount(1, $response['addConfigurableProductsToCart']['cart']['items']);
392+
$item = $response['addConfigurableProductsToCart']['cart']['items'][0];
393+
$this->assertEquals($sku, $item['product']['sku']);
394+
$expectedOptions = [
395+
'configurable_options' => [
396+
[
397+
'option_label' => 'Test Configurable',
398+
'value_label' => 'Option 1'
399+
]
400+
],
401+
'customizable_options' => [
402+
[
403+
'id' => $optionId,
404+
'label' => 'Dropdown Options',
405+
'values' => [
406+
[
407+
'label' => 'Option 2',
408+
'value' => $optionValueId
409+
]
410+
]
411+
]
412+
]
413+
];
414+
415+
$this->assertResponseFields($item['configurable_options'], $expectedOptions['configurable_options']);
416+
$this->assertResponseFields($item['customizable_options'], $expectedOptions['customizable_options']);
417+
}
418+
331419
/**
332420
* @param string $maskedQuoteId
333421
* @param string $parentSku
@@ -406,4 +494,39 @@ private function getFetchProductQuery(string $term): string
406494
}
407495
QUERY;
408496
}
497+
498+
/**
499+
* Get product customizable dropdown options
500+
*
501+
* @param string $productSku
502+
* @return array
503+
*/
504+
private function getAvailableProductCustomOption(string $productSku): array
505+
{
506+
$query = <<<QUERY
507+
{
508+
products(filter: {sku: {eq: "${productSku}"}}) {
509+
items {
510+
name
511+
... on CustomizableProductInterface {
512+
options {
513+
option_id
514+
title
515+
... on CustomizableDropDownOption {
516+
value {
517+
option_type_id
518+
title
519+
}
520+
}
521+
}
522+
}
523+
}
524+
}
525+
}
526+
QUERY;
527+
528+
$response = $this->graphQlQuery($query);
529+
$this->assertNotEmpty($response['products']['items'], "No result for product with sku: '{$productSku}'");
530+
return $response['products']['items'][0]['options'];
531+
}
409532
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
13+
14+
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/product_configurable.php');
15+
16+
/** @var ObjectManagerInterface $objectManager */
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var ProductCustomOptionInterfaceFactory $optionRepository */
19+
$optionRepository = $objectManager->get(ProductCustomOptionInterfaceFactory::class);
20+
/** @var ProductRepositoryInterface $productRepository */
21+
$productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
22+
$product = $productRepository->get('configurable');
23+
$dropdownOption = [
24+
'previous_group' => 'select',
25+
'title' => 'Dropdown Options',
26+
'type' => 'drop_down',
27+
'is_require' => 1,
28+
'sort_order' => 0,
29+
'values' => [
30+
[
31+
'option_type_id' => null,
32+
'title' => 'Option 1',
33+
'price' => '10.00',
34+
'price_type' => 'fixed',
35+
'sku' => 'opt1',
36+
],
37+
[
38+
'option_type_id' => null,
39+
'title' => 'Option 2',
40+
'price' => '20.00',
41+
'price_type' => 'fixed',
42+
'sku' => 'opt2',
43+
],
44+
]
45+
];
46+
47+
$createdOption = $optionRepository->create(['data' => $dropdownOption]);
48+
$createdOption->setProductSku($product->getSku());
49+
$product->setOptions([$createdOption]);
50+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
9+
10+
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/product_configurable_rollback.php');

0 commit comments

Comments
 (0)