diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php
index ad730288e5cc..b491b10730c1 100644
--- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php
+++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php
@@ -13,21 +13,13 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
-use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Model\Quote;
/**
* Add simple product to cart
- *
- * TODO: should be replaced for different types resolver
*/
class AddSimpleProductToCart
{
- /**
- * @var ArrayManager
- */
- private $arrayManager;
-
/**
* @var DataObjectFactory
*/
@@ -39,16 +31,13 @@ class AddSimpleProductToCart
private $productRepository;
/**
- * @param ArrayManager $arrayManager
* @param DataObjectFactory $dataObjectFactory
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
- ArrayManager $arrayManager,
DataObjectFactory $dataObjectFactory,
ProductRepositoryInterface $productRepository
) {
- $this->arrayManager = $arrayManager;
$this->dataObjectFactory = $dataObjectFactory;
$this->productRepository = $productRepository;
}
@@ -67,11 +56,6 @@ public function execute(Quote $cart, array $cartItemData): void
{
$sku = $this->extractSku($cartItemData);
$quantity = $this->extractQuantity($cartItemData);
- if ($quantity <= 0) {
- throw new GraphQlInputException(
- __('Please enter a number greater than 0 in this field.')
- );
- }
$customizableOptions = $this->extractCustomizableOptions($cartItemData);
try {
@@ -105,11 +89,10 @@ public function execute(Quote $cart, array $cartItemData): void
*/
private function extractSku(array $cartItemData): string
{
- $sku = $this->arrayManager->get('data/sku', $cartItemData);
- if (!isset($sku)) {
- throw new GraphQlInputException(__('Missing key "sku" in cart item data'));
+ if (!isset($cartItemData['data']['sku']) || empty($cartItemData['data']['sku'])) {
+ throw new GraphQlInputException(__('Missed "sku" in cart item data'));
}
- return (string)$sku;
+ return (string)$cartItemData['data']['sku'];
}
/**
@@ -121,11 +104,17 @@ private function extractSku(array $cartItemData): string
*/
private function extractQuantity(array $cartItemData): float
{
- $quantity = $this->arrayManager->get('data/quantity', $cartItemData);
- if (!isset($quantity)) {
- throw new GraphQlInputException(__('Missing key "quantity" in cart item data'));
+ if (!isset($cartItemData['data']['quantity'])) {
+ throw new GraphQlInputException(__('Missed "qty" in cart item data'));
+ }
+ $quantity = (float)$cartItemData['data']['quantity'];
+
+ if ($quantity <= 0) {
+ throw new GraphQlInputException(
+ __('Please enter a number greater than 0 in this field.')
+ );
}
- return (float)$quantity;
+ return $quantity;
}
/**
@@ -136,11 +125,17 @@ private function extractQuantity(array $cartItemData): float
*/
private function extractCustomizableOptions(array $cartItemData): array
{
- $customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
+ if (!isset($cartItemData['customizable_options']) || empty($cartItemData['customizable_options'])) {
+ return [];
+ }
$customizableOptionsData = [];
- foreach ($customizableOptions as $customizableOption) {
- $customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
+ foreach ($cartItemData['customizable_options'] as $customizableOption) {
+ if (isset($customizableOption['value_string'])) {
+ $customizableOptionsData[$customizableOption['id']] = $this->convertCustomOptionValue(
+ $customizableOption['value_string']
+ );
+ }
}
return $customizableOptionsData;
}
@@ -161,4 +156,20 @@ private function createBuyRequest(float $quantity, array $customOptions): DataOb
],
]);
}
+
+ /**
+ * Convert custom options vakue
+ *
+ * @param string $value
+ * @return string|array
+ */
+ private function convertCustomOptionValue(string $value)
+ {
+ $value = trim($value);
+ if (substr($value, 0, 1) === "[" &&
+ substr($value, strlen($value) - 1, 1) === "]") {
+ return explode(',', substr($value, 1, -1));
+ }
+ return $value;
+ }
}
diff --git a/app/code/Magento/QuoteGraphQl/etc/di.xml b/app/code/Magento/QuoteGraphQl/etc/di.xml
index 0697761a2a2a..32b8b12987ff 100644
--- a/app/code/Magento/QuoteGraphQl/etc/di.xml
+++ b/app/code/Magento/QuoteGraphQl/etc/di.xml
@@ -26,7 +26,7 @@
- Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Text
- Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown
- Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown
- - Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown
+ - Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple
- Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple
diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls
index e2d29e93db57..c2b92034c329 100644
--- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls
+++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls
@@ -52,7 +52,7 @@ input CartItemInput {
input CustomizableOptionInput {
id: Int!
- value: String!
+ value_string: String!
}
input ApplyCouponToCartInput {
@@ -314,8 +314,7 @@ interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\
type SelectedCustomizableOption {
id: Int!
label: String!
- type: String!
- is_required: Int!
+ is_required: Boolean!
values: [SelectedCustomizableOptionValue!]!
sort_order: Int!
}
@@ -323,9 +322,8 @@ type SelectedCustomizableOption {
type SelectedCustomizableOptionValue {
id: Int!
label: String!
- value: String!
+ value: String
price: CartItemSelectedOptionValuePrice!
- sort_order: Int!
}
type CartItemSelectedOptionValuePrice {
diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php
index 63546298304b..d64c51354129 100644
--- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php
+++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductWithCustomOptionsToCartTest.php
@@ -50,41 +50,11 @@ public function testAddSimpleProductWithOptions()
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$customOptionsValues = $this->getCustomOptionsValuesForQuery($sku);
-
/* Generate customizable options fragment for GraphQl request */
- $queryCustomizableOptions = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
+ $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
- $query = <<getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
$response = $this->graphQlMutation($query);
@@ -95,7 +65,7 @@ public function testAddSimpleProductWithOptions()
$assignedOptionsCount = count($customOptionsValues);
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
self::assertEquals(
- $customOptionsValues[$counter]['value'],
+ $customOptionsValues[$counter]['value_string'],
$customizableOptionsOutput[$counter]['values'][0]['value']
);
}
@@ -107,13 +77,31 @@ public function testAddSimpleProductWithOptions()
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
- public function testAddSimpleProductWithNoRequiredOptionsSet()
+ public function testAddSimpleProductWithMissedRequiredOptionsSet()
{
+ $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = 'simple';
$quantity = 1;
- $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
+ $customizableOptions = '';
- $query = <<getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
+
+ self::expectExceptionMessage(
+ 'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.'
+ );
+ $this->graphQlMutation($query);
+ }
+
+ /**
+ * @param string $maskedQuoteId
+ * @param string $sku
+ * @param float $quantity
+ * @param string $customizableOptions
+ * @return string
+ */
+ private function getQuery(string $maskedQuoteId, string $sku, float $quantity, string $customizableOptions): string
+ {
+ return <<graphQlMutation($query);
}
/**
@@ -168,13 +151,13 @@ private function getCustomOptionsValuesForQuery(string $sku): array
if ($optionType == 'field' || $optionType == 'area') {
$customOptionsValues[] = [
'id' => (int) $customOption->getOptionId(),
- 'value' => 'test'
+ 'value_string' => 'test'
];
} elseif ($optionType == 'drop_down') {
$optionSelectValues = $customOption->getValues();
$customOptionsValues[] = [
'id' => (int) $customOption->getOptionId(),
- 'value' => reset($optionSelectValues)->getOptionTypeId()
+ 'value_string' => reset($optionSelectValues)->getOptionTypeId()
];
}
}
diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php
index 94ac11ad8e0b..55542919cc09 100644
--- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php
+++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddVirtualProductWithCustomOptionsToCartTest.php
@@ -50,41 +50,11 @@ public function testAddVirtualProductWithOptions()
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$customOptionsValues = $this->getCustomOptionsValuesForQuery($sku);
-
/* Generate customizable options fragment for GraphQl request */
- $queryCustomizableOptions = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
+ $queryCustomizableOptionValues = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));
- $query = <<getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
$response = $this->graphQlMutation($query);
@@ -95,7 +65,7 @@ public function testAddVirtualProductWithOptions()
$assignedOptionsCount = count($customOptionsValues);
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
self::assertEquals(
- $customOptionsValues[$counter]['value'],
+ $customOptionsValues[$counter]['value_string'],
$customizableOptionsOutput[$counter]['values'][0]['value']
);
}
@@ -107,13 +77,31 @@ public function testAddVirtualProductWithOptions()
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual_with_options.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
- public function testAddVirtualProductWithNoRequiredOptionsSet()
+ public function testAddSimpleProductWithMissedRequiredOptionsSet()
{
+ $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
$sku = 'virtual';
$quantity = 1;
- $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
+ $customizableOptions = '';
- $query = <<getQuery($maskedQuoteId, $sku, $quantity, $customizableOptions);
+
+ self::expectExceptionMessage(
+ 'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.'
+ );
+ $this->graphQlMutation($query);
+ }
+
+ /**
+ * @param string $maskedQuoteId
+ * @param string $sku
+ * @param float $quantity
+ * @param string $customizableOptions
+ * @return string
+ */
+ private function getQuery(string $maskedQuoteId, string $sku, float $quantity, string $customizableOptions): string
+ {
+ return <<graphQlMutation($query);
}
/**
@@ -168,13 +151,13 @@ private function getCustomOptionsValuesForQuery(string $sku): array
if ($optionType == 'field' || $optionType == 'area') {
$customOptionsValues[] = [
'id' => (int) $customOption->getOptionId(),
- 'value' => 'test'
+ 'value_string' => 'test'
];
} elseif ($optionType == 'drop_down') {
$optionSelectValues = $customOption->getValues();
$customOptionsValues[] = [
'id' => (int) $customOption->getOptionId(),
- 'value' => reset($optionSelectValues)->getOptionTypeId()
+ 'value_string' => reset($optionSelectValues)->getOptionTypeId()
];
}
}