From 0e8cf9246461f6cfdbec4d064fe557ad29fcd536 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Wed, 24 Apr 2019 15:50:27 +0300 Subject: [PATCH 1/8] Update cart items with customizable options --- .../Model/Cart/UpdateCartItem.php | 98 +++++++++++++++++++ .../Model/Resolver/UpdateCartItems.php | 61 +++++++++--- .../Magento/QuoteGraphQl/etc/schema.graphqls | 3 +- 3 files changed, 150 insertions(+), 12 deletions(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php new file mode 100644 index 0000000000000..bc98eb93b55cc --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php @@ -0,0 +1,98 @@ +dataObjectFactory = $dataObjectFactory; + } + + /** + * Update cart item + * + * @param Quote $cart + * @param int $cartItemId + * @param float $qty + * @param null $customizableOptionsData + * @return void + * @throws GraphQlInputException + */ + public function execute(Quote $cart, int $cartItemId, float $qty, array $customizableOptionsData): void + { + $customizableOptions = []; + foreach ($customizableOptionsData as $customizableOption) { + $customizableOptions[$customizableOption['id']] = $customizableOption['value']; + } + + try { + $result = $cart->updateItem( + $cartItemId, + $this->createBuyRequest($qty, $customizableOptions) + ); + } catch (LocalizedException $e) { + throw new GraphQlInputException( + __( + 'Could not update cart item: %message', + ['message' => $e->getMessage()] + ) + ); + } + + if (is_string($result)) { + throw new GraphQlInputException(__( + 'Could not update cart item: %message', + ['message' => $result] + )); + } + + if ($result->getHasError()) { + throw new GraphQlInputException(__( + 'Could not update cart item: %message', + ['message' => $result->getMessage()] + )); + } + } + + /** + * Format GraphQl input data to a shape that buy request has + * + * @param float $qty + * @param array $customOptions + * @return DataObject + */ + private function createBuyRequest(float $qty, array $customOptions): DataObject + { + return $this->dataObjectFactory->create([ + 'data' => [ + 'qty' => $qty, + 'options' => $customOptions, + ], + ]); + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php index 78a07506556c0..ed1cb7a9dab50 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php @@ -15,14 +15,26 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Quote\Api\CartItemRepositoryInterface; +use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; +use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem; /** * @inheritdoc */ class UpdateCartItems implements ResolverInterface { + /** + * @var CartRepositoryInterface + */ + private $quoteRepository; + + /** + * @var UpdateCartItem + */ + private $updateCartItem; + /** * @var GetCartForUser */ @@ -36,13 +48,19 @@ class UpdateCartItems implements ResolverInterface /** * @param GetCartForUser $getCartForUser * @param CartItemRepositoryInterface $cartItemRepository + * @param UpdateCartItem $updateCartItem + * @param CartRepositoryInterface $quoteRepository */ public function __construct( GetCartForUser $getCartForUser, - CartItemRepositoryInterface $cartItemRepository + CartItemRepositoryInterface $cartItemRepository, + UpdateCartItem $updateCartItem, + CartRepositoryInterface $quoteRepository ) { $this->getCartForUser = $getCartForUser; $this->cartItemRepository = $cartItemRepository; + $this->updateCartItem = $updateCartItem; + $this->quoteRepository = $quoteRepository; } /** @@ -93,26 +111,47 @@ private function processCartItems(Quote $cart, array $items): void if (!isset($item['cart_item_id']) || empty($item['cart_item_id'])) { throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.')); } - $itemId = $item['cart_item_id']; + $itemId = (int)$item['cart_item_id']; if (!isset($item['quantity'])) { throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.')); } $qty = (float)$item['quantity']; - $cartItem = $cart->getItemById($itemId); - if ($cartItem === false) { - throw new GraphQlNoSuchEntityException( - __('Could not find cart item with id: %1.', $item['cart_item_id']) - ); - } - if ($qty <= 0.0) { $this->cartItemRepository->deleteById((int)$cart->getId(), $itemId); } else { - $cartItem->setQty($qty); - $this->cartItemRepository->save($cartItem); + $customizableOptions = $item['customizable_options'] ?? null; + + if ($customizableOptions === null) { // Update only item's qty + $this->updateItemQty($itemId, $cart, $qty); + } else { // Update customizable options (and QTY if changed) + $this->updateCartItem->execute($cart, $itemId, $qty, $customizableOptions); + $this->quoteRepository->save($cart); + } } } } + + /** + * Updates item qty for the specified cart + * + * @param int $itemId + * @param Quote $cart + * @param float $qty + * @throws GraphQlNoSuchEntityException + * @throws NoSuchEntityException + * @throws GraphQlNoSuchEntityException + */ + private function updateItemQty(int $itemId, Quote $cart, float $qty) + { + $cartItem = $cart->getItemById($itemId); + if ($cartItem === false) { + throw new GraphQlNoSuchEntityException( + __('Could not find cart item with id: %1.', $itemId) + ); + } + $cartItem->setQty($qty); + $this->cartItemRepository->save($cartItem); + } } diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index a9784e97c8952..afbb8ceb03b39 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -67,7 +67,8 @@ input UpdateCartItemsInput { input CartItemQuantityInput { cart_item_id: Int! - quantity: Float! + quantity: Float + customizable_options: [CustomizableOptionInput!] } input RemoveItemFromCartInput { From 62e053068a824ef81fa0c7c268c0b2b140976514 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Wed, 8 May 2019 11:44:50 +0200 Subject: [PATCH 2/8] Added API-functional tests --- .../Model/Resolver/UpdateCartItems.php | 8 +- .../Quote/EditQuoteItemCustomOptionsTest.php | 275 ++++++++++++++++++ .../set_custom_options_simple_product.php | 54 ++++ .../add_simple_product_with_options.php | 51 ++++ 4 files changed, 384 insertions(+), 4 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php create mode 100644 dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php index ed1cb7a9dab50..ddd713f8008fd 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php @@ -116,17 +116,17 @@ private function processCartItems(Quote $cart, array $items): void if (!isset($item['quantity'])) { throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.')); } - $qty = (float)$item['quantity']; + $quantity = (float)$item['quantity']; - if ($qty <= 0.0) { + if ($quantity <= 0.0) { $this->cartItemRepository->deleteById((int)$cart->getId(), $itemId); } else { $customizableOptions = $item['customizable_options'] ?? null; if ($customizableOptions === null) { // Update only item's qty - $this->updateItemQty($itemId, $cart, $qty); + $this->updateItemQty($itemId, $cart, $quantity); } else { // Update customizable options (and QTY if changed) - $this->updateCartItem->execute($cart, $itemId, $qty, $customizableOptions); + $this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions); $this->quoteRepository->save($cart); } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php new file mode 100644 index 0000000000000..09495f92648be --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php @@ -0,0 +1,275 @@ +getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); + $this->productCustomOptionsRepository = $objectManager->get(ProductCustomOptionRepositoryInterface::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteResource = $objectManager->get(QuoteResource::class); + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); + } + + /** + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product_with_options.php + */ + public function testChangeQuoteItemCustomOptions() + { + $sku = 'simple_product'; + $quoteItemId = $this->getQuoteItemIdBySku($sku); + $customOptionsValues = $this->getCustomOptionsValuesForQuery($sku); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $customizableOptionsQuery = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + + $query = $this->getQuery($maskedQuoteId, $quoteItemId, $customizableOptionsQuery); + $response = $this->graphQlMutation($query); + $itemOptionsResponse = $response['updateCartItems']['cart']['items'][0]['customizable_options']; + self::assertCount(2, $itemOptionsResponse); + self::assertEquals('test', $itemOptionsResponse[0]['values'][0]['value']); + self::assertEquals('test', $itemOptionsResponse[1]['values'][0]['value']); + } + + /** + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product_with_options.php + */ + public function testOptionsSetPersistsOnQtyChange() + { + $sku = 'simple_product'; + $newQuantity = 2; + $quoteItemId = $this->getQuoteItemIdBySku($sku); + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + + $query = <<graphQlMutation($query); + $cartItemResponse = $response['updateCartItems']['cart']['items'][0]; + + self::assertEquals($newQuantity, $cartItemResponse['quantity']); + self::assertCount(2, $cartItemResponse['customizable_options']); + self::assertEquals('initial value', $cartItemResponse['customizable_options'][0]['values'][0]['value']); + self::assertEquals('initial value', $cartItemResponse['customizable_options'][1]['values'][0]['value']); + } + + /** + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product_with_options.php + */ + public function testOptionsSetChangedOnChangeOneOption() + { + $sku = 'simple_product'; + $quoteItemId = $this->getQuoteItemIdBySku($sku); + + /* Get only the first option */ + $customOptionsValues = array_slice($this->getCustomOptionsValuesForQuery($sku), 0, 1); + + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $customizableOptionsQuery = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $query = $this->getQuery($maskedQuoteId, $quoteItemId, $customizableOptionsQuery); + + $response = $this->graphQlMutation($query); + $itemOptionsResponse = $response['updateCartItems']['cart']['items'][0]['customizable_options']; + self::assertCount(1, $itemOptionsResponse); + self::assertEquals('test', $itemOptionsResponse[0]['values'][0]['value']); + } + + /** + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product_with_options.php + * @group recent + */ + public function testOptionSetPersistsOnExtraOptionWithIncorrectId() + { + $sku = 'simple_product'; + $quoteItemId = $this->getQuoteItemIdBySku($sku); + $customOptionsValues = $this->getCustomOptionsValuesForQuery($sku); + + /* Add nonexistent option to the query */ + $customOptionsValues[] = ['id' => -10, 'value' => 'value']; + + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $customizableOptionsQuery = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); + $query = $this->getQuery($maskedQuoteId, $quoteItemId, $customizableOptionsQuery); + + $response = $this->graphQlMutation($query); + $itemOptionsResponse = $response['updateCartItems']['cart']['items'][0]['customizable_options']; + self::assertCount(2, $itemOptionsResponse); + } + + /** + * Returns GraphQl query for updating items in shopping cart + * + * @param string $maskedQuoteId + * @param int $quoteItemId + * @param $customizableOptionsQuery + * @return string + */ + private function getQuery(string $maskedQuoteId, int $quoteItemId, $customizableOptionsQuery): string + { + return <<quoteFactory->create(); + $product = $this->productRepository->get($sku); + $this->quoteResource->load($quote, 'test_quote', 'reserved_order_id'); + /** @var Item $quoteItem */ + $quoteItem = $quote->getItemByProduct($product); + + return (int)$quoteItem->getId(); + } + + + /** + * Generate an array with test values for customizable options + * based on the option type + * + * @param string $sku + * @return array + */ + private function getCustomOptionsValuesForQuery(string $sku): array + { + $customOptions = $this->productCustomOptionsRepository->getList($sku); + $customOptionsValues = []; + + foreach ($customOptions as $customOption) { + $optionType = $customOption->getType(); + if ($optionType == 'field' || $optionType == 'area') { + $customOptionsValues[] = [ + 'id' => (int) $customOption->getOptionId(), + 'value' => 'test' + ]; + } elseif ($optionType == 'drop_down') { + $optionSelectValues = $customOption->getValues(); + $customOptionsValues[] = [ + 'id' => (int) $customOption->getOptionId(), + 'value' => reset($optionSelectValues)->getOptionTypeId() + ]; + } + } + + return $customOptionsValues; + } +} diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php b/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php new file mode 100644 index 0000000000000..618ed0e25602f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php @@ -0,0 +1,54 @@ + 'test_option_code_1', + 'type' => 'field', + 'is_require' => false, + 'sort_order' => 1, + 'price' => -10.0, + 'price_type' => 'fixed', + 'sku' => 'sku1', + 'max_characters' => 100, + ], + [ + 'title' => 'area option', + 'type' => 'area', + 'is_require' => false, + 'sort_order' => 2, + 'price' => 20.0, + 'price_type' => 'percent', + 'sku' => 'sku2', + 'max_characters' => 100 + ] +]; + +/** @var ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->get(ProductRepositoryInterface::class); +$product = $productRepository->get('simple_product'); +/** @var ProductCustomOptionInterfaceFactory $customOptionFactory */ +$customOptionFactory = $objectManager->get(ProductCustomOptionInterfaceFactory::class); + +foreach ($optionsSet as $option) { + /** @var ProductCustomOptionInterface $customOption */ + $customOption = $customOptionFactory->create(['data' => $option]); + $customOption->setProductSku($product->getSku()); + + $productCustomOptions[] = $customOption; +} + +$product->setOptions($productCustomOptions); +$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php new file mode 100644 index 0000000000000..68e2261ab02b0 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php @@ -0,0 +1,51 @@ +get(ProductRepositoryInterface::class); +/** @var QuoteFactory $quoteFactory */ +$quoteFactory = Bootstrap::getObjectManager()->get(QuoteFactory::class); +/** @var QuoteResource $quoteResource */ +$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class); +/** @var CartRepositoryInterface $cartRepository */ +$cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class); +/** @var OptionFactory $productOptionFactory */ +$productOptionFactory = Bootstrap::getObjectManager()->get(OptionFactory::class); +/** @var DataObjectFactory $dataObjectFactory */ +$dataObjectFactory = Bootstrap::getObjectManager()->get(DataObjectFactory::class); + +/** @var ProductOption $productOption */ +$productOption = $productOptionFactory->create(); +$product = $productRepository->get('simple_product'); +$productOptions = $productOption->getProductOptionCollection($product); +$cartItemCustomOptions = []; + +/** @var ProductOption $productOption */ +foreach ($productOptions as $productOption) { + $cartItemCustomOptions[$productOption->getId()] = 'initial value'; +} + +$request = $dataObjectFactory->create([ + 'data' => [ + 'qty' => 1.0, + 'options' => $cartItemCustomOptions, + ], +]); + +$quote = $quoteFactory->create(); +$quoteResource->load($quote, 'test_quote', 'reserved_order_id'); +$quote->addProduct($product, $request); +$cartRepository->save($quote); From 7d9a1330a5bd3e351efc5ff33107f7e0466c580a Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Wed, 8 May 2019 12:23:22 +0200 Subject: [PATCH 3/8] Adjusted option values according to the latest changes --- app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php | 2 +- .../GraphQl/Quote/EditQuoteItemCustomOptionsTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php index bc98eb93b55cc..ac9b464eb01b0 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php @@ -47,7 +47,7 @@ public function execute(Quote $cart, int $cartItemId, float $qty, array $customi { $customizableOptions = []; foreach ($customizableOptionsData as $customizableOption) { - $customizableOptions[$customizableOption['id']] = $customizableOption['value']; + $customizableOptions[$customizableOption['id']] = $customizableOption['value_string']; } try { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php index 09495f92648be..d3bd10d8ae00e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php @@ -168,7 +168,7 @@ public function testOptionSetPersistsOnExtraOptionWithIncorrectId() $customOptionsValues = $this->getCustomOptionsValuesForQuery($sku); /* Add nonexistent option to the query */ - $customOptionsValues[] = ['id' => -10, 'value' => 'value']; + $customOptionsValues[] = ['id' => -10, 'value_string' => 'value']; $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $customizableOptionsQuery = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues)); @@ -259,13 +259,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() ]; } } From c0c04ffa3e34973df811bfbc2c9c7e480d666cd6 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Wed, 8 May 2019 12:28:44 +0200 Subject: [PATCH 4/8] Return cart item error message as a string --- app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php index ac9b464eb01b0..81ed4f23c4414 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php @@ -74,7 +74,7 @@ public function execute(Quote $cart, int $cartItemId, float $qty, array $customi if ($result->getHasError()) { throw new GraphQlInputException(__( 'Could not update cart item: %message', - ['message' => $result->getMessage()] + ['message' => $result->getMessage(true)] )); } } From 91251eacad024163a73632c5ffeb55a9c850aba4 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sat, 11 May 2019 12:45:27 -0700 Subject: [PATCH 5/8] Fixed tests, decoupled logic for updating items --- .../Model/Cart/UpdateCartItem.php | 83 ++++++++++++++++++- .../Model/Resolver/UpdateCartItems.php | 45 +--------- .../Quote/Customer/UpdateCartItemsTest.php | 2 +- .../Quote/EditQuoteItemCustomOptionsTest.php | 1 - .../Quote/Guest/UpdateCartItemsTest.php | 2 +- 5 files changed, 87 insertions(+), 46 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php index 81ed4f23c4414..5178f26e1dc93 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php @@ -10,8 +10,13 @@ use Magento\Framework\DataObject; use Magento\Framework\DataObjectFactory; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; +use Magento\Quote\Api\CartItemRepositoryInterface; +use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; +use Magento\Quote\Model\Quote\Item; /** * Update cart item @@ -19,6 +24,16 @@ */ class UpdateCartItem { + /** + * @var CartRepositoryInterface + */ + private $quoteRepository; + + /** + * @var CartItemRepositoryInterface + */ + private $cartItemRepository; + /** * @var DataObjectFactory */ @@ -26,11 +41,17 @@ class UpdateCartItem /** * @param DataObjectFactory $dataObjectFactory + * @param CartItemRepositoryInterface $cartItemRepository + * @param CartRepositoryInterface $quoteRepository */ public function __construct( - DataObjectFactory $dataObjectFactory + DataObjectFactory $dataObjectFactory, + CartItemRepositoryInterface $cartItemRepository, + CartRepositoryInterface $quoteRepository ) { $this->dataObjectFactory = $dataObjectFactory; + $this->cartItemRepository = $cartItemRepository; + $this->quoteRepository = $quoteRepository; } /** @@ -39,12 +60,20 @@ public function __construct( * @param Quote $cart * @param int $cartItemId * @param float $qty - * @param null $customizableOptionsData + * @param array $customizableOptionsData * @return void * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + * @throws NoSuchEntityException */ public function execute(Quote $cart, int $cartItemId, float $qty, array $customizableOptionsData): void { + if (count($customizableOptionsData) === 0) { // Update only item's qty + $this->updateItemQty($cartItemId, $cart, $qty); + + return; + } + $customizableOptions = []; foreach ($customizableOptionsData as $customizableOption) { $customizableOptions[$customizableOption['id']] = $customizableOption['value_string']; @@ -77,6 +106,56 @@ public function execute(Quote $cart, int $cartItemId, float $qty, array $customi ['message' => $result->getMessage(true)] )); } + + $this->quoteRepository->save($cart); + } + + /** + * Updates item qty for the specified cart + * + * @param int $itemId + * @param Quote $cart + * @param float $qty + * @throws GraphQlNoSuchEntityException + * @throws NoSuchEntityException + * @throws GraphQlNoSuchEntityException + */ + private function updateItemQty(int $itemId, Quote $cart, float $qty) + { + $cartItem = $cart->getItemById($itemId); + if ($cartItem === false) { + throw new GraphQlNoSuchEntityException( + __('Could not find cart item with id: %1.', $itemId) + ); + } + $cartItem->setQty($qty); + $this->validateCartItem($cartItem); + $this->cartItemRepository->save($cartItem); + } + + /** + * Validate cart item + * + * @param Item $cartItem + * @return void + * @throws GraphQlInputException + */ + private function validateCartItem(Item $cartItem): void + { + if ($cartItem->getHasError()) { + $errors = []; + foreach ($cartItem->getMessage(false) as $message) { + $errors[] = $message; + } + if (!empty($errors)) { + throw new GraphQlInputException( + __( + 'Could not update the product with SKU %sku: %message', + ['sku' => $cartItem->getSku(), 'message' => __(implode("\n", $errors))] + ) + ); + } + } } /** diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php index ddd713f8008fd..79738a40a7fb6 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php @@ -15,7 +15,6 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Quote\Api\CartItemRepositoryInterface; -use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem; @@ -25,11 +24,6 @@ */ class UpdateCartItems implements ResolverInterface { - /** - * @var CartRepositoryInterface - */ - private $quoteRepository; - /** * @var UpdateCartItem */ @@ -49,18 +43,15 @@ class UpdateCartItems implements ResolverInterface * @param GetCartForUser $getCartForUser * @param CartItemRepositoryInterface $cartItemRepository * @param UpdateCartItem $updateCartItem - * @param CartRepositoryInterface $quoteRepository */ public function __construct( GetCartForUser $getCartForUser, CartItemRepositoryInterface $cartItemRepository, - UpdateCartItem $updateCartItem, - CartRepositoryInterface $quoteRepository + UpdateCartItem $updateCartItem ) { $this->getCartForUser = $getCartForUser; $this->cartItemRepository = $cartItemRepository; $this->updateCartItem = $updateCartItem; - $this->quoteRepository = $quoteRepository; } /** @@ -112,8 +103,9 @@ private function processCartItems(Quote $cart, array $items): void throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.')); } $itemId = (int)$item['cart_item_id']; + $customizableOptions = $item['customizable_options'] ?? []; - if (!isset($item['quantity'])) { + if (count($customizableOptions) === 0 && !isset($item['quantity'])) { throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.')); } $quantity = (float)$item['quantity']; @@ -121,37 +113,8 @@ private function processCartItems(Quote $cart, array $items): void if ($quantity <= 0.0) { $this->cartItemRepository->deleteById((int)$cart->getId(), $itemId); } else { - $customizableOptions = $item['customizable_options'] ?? null; - - if ($customizableOptions === null) { // Update only item's qty - $this->updateItemQty($itemId, $cart, $quantity); - } else { // Update customizable options (and QTY if changed) - $this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions); - $this->quoteRepository->save($cart); - } + $this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions); } } } - - /** - * Updates item qty for the specified cart - * - * @param int $itemId - * @param Quote $cart - * @param float $qty - * @throws GraphQlNoSuchEntityException - * @throws NoSuchEntityException - * @throws GraphQlNoSuchEntityException - */ - private function updateItemQty(int $itemId, Quote $cart, float $qty) - { - $cartItem = $cart->getItemById($itemId); - if ($cartItem === false) { - throw new GraphQlNoSuchEntityException( - __('Could not find cart item with id: %1.', $itemId) - ); - } - $cartItem->setQty($qty); - $this->cartItemRepository->save($cartItem); - } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/UpdateCartItemsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/UpdateCartItemsTest.php index bc88bd1ddb438..48ea4ab7a15e3 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/UpdateCartItemsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/UpdateCartItemsTest.php @@ -285,7 +285,7 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array ], 'missed_cart_item_qty' => [ 'cart_items: [{ cart_item_id: 1 }]', - 'Field CartItemUpdateInput.quantity of required type Float! was not provided.' + 'Required parameter "quantity" for "cart_items" is missing.' ], ]; } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php index d3bd10d8ae00e..1aaa23860f5f1 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php @@ -159,7 +159,6 @@ public function testOptionsSetChangedOnChangeOneOption() * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_custom_options_simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product_with_options.php - * @group recent */ public function testOptionSetPersistsOnExtraOptionWithIncorrectId() { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php index 1e1fb0a176992..988ead7d86df3 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/UpdateCartItemsTest.php @@ -236,7 +236,7 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array ], 'missed_cart_item_qty' => [ 'cart_items: [{ cart_item_id: 1 }]', - 'Field CartItemUpdateInput.quantity of required type Float! was not provided.' + 'Required parameter "quantity" for "cart_items" is missing.' ], ]; } From ba64fbf818bc74fddda792d5357e0dc848ced54a Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 16 May 2019 19:53:58 -0700 Subject: [PATCH 6/8] Added support for multiselect and checkbox values --- .../Model/Cart/UpdateCartItem.php | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php index 5178f26e1dc93..5d166ec882875 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php @@ -76,7 +76,9 @@ public function execute(Quote $cart, int $cartItemId, float $qty, array $customi $customizableOptions = []; foreach ($customizableOptionsData as $customizableOption) { - $customizableOptions[$customizableOption['id']] = $customizableOption['value_string']; + $customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue( + $customizableOption['value_string'] + ); } try { @@ -174,4 +176,23 @@ private function createBuyRequest(float $qty, array $customOptions): DataObject ], ]); } + + // TODO: Refactor the code duplication with addCartItem + // TODO: Make a reusable logic that is shared between add to cart / change cart approaches + + /** + * 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; + } } From 1cec8759784b6c87bfd59833ff304addfa0fbbf1 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sun, 19 May 2019 16:08:59 +0200 Subject: [PATCH 7/8] Refactoring --- .../Model/Cart/AddSimpleProductToCart.php | 51 ++---------- .../Model/Cart/CreateBuyRequest.php | 73 ++++++++++++++++++ .../Model/Cart/UpdateCartItem.php | 77 ++++--------------- ...=> EditQuoteItemWithCustomOptionsTest.php} | 2 +- 4 files changed, 98 insertions(+), 105 deletions(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php rename dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/{EditQuoteItemCustomOptionsTest.php => EditQuoteItemWithCustomOptionsTest.php} (99%) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index b491b10730c10..0bf7968a50fae 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -8,8 +8,6 @@ namespace Magento\QuoteGraphQl\Model\Cart; use Magento\Catalog\Api\ProductRepositoryInterface; -use Magento\Framework\DataObject; -use Magento\Framework\DataObjectFactory; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; @@ -21,9 +19,9 @@ class AddSimpleProductToCart { /** - * @var DataObjectFactory + * @var CreateBuyRequest */ - private $dataObjectFactory; + private $createBuyRequest; /** * @var ProductRepositoryInterface @@ -31,15 +29,15 @@ class AddSimpleProductToCart private $productRepository; /** - * @param DataObjectFactory $dataObjectFactory * @param ProductRepositoryInterface $productRepository + * @param CreateBuyRequest $createBuyRequest */ public function __construct( - DataObjectFactory $dataObjectFactory, - ProductRepositoryInterface $productRepository + ProductRepositoryInterface $productRepository, + CreateBuyRequest $createBuyRequest ) { - $this->dataObjectFactory = $dataObjectFactory; $this->productRepository = $productRepository; + $this->createBuyRequest = $createBuyRequest; } /** @@ -56,7 +54,7 @@ public function execute(Quote $cart, array $cartItemData): void { $sku = $this->extractSku($cartItemData); $quantity = $this->extractQuantity($cartItemData); - $customizableOptions = $this->extractCustomizableOptions($cartItemData); + $customizableOptions = $cartItemData['customizable_options'] ?? []; try { $product = $this->productRepository->get($sku); @@ -65,7 +63,7 @@ public function execute(Quote $cart, array $cartItemData): void } try { - $result = $cart->addProduct($product, $this->createBuyRequest($quantity, $customizableOptions)); + $result = $cart->addProduct($product, $this->createBuyRequest->execute($quantity, $customizableOptions)); } catch (\Exception $e) { throw new GraphQlInputException( __( @@ -139,37 +137,4 @@ private function extractCustomizableOptions(array $cartItemData): array } return $customizableOptionsData; } - - /** - * Format GraphQl input data to a shape that buy request has - * - * @param float $quantity - * @param array $customOptions - * @return DataObject - */ - private function createBuyRequest(float $quantity, array $customOptions): DataObject - { - return $this->dataObjectFactory->create([ - 'data' => [ - 'qty' => $quantity, - 'options' => $customOptions, - ], - ]); - } - - /** - * 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/Model/Cart/CreateBuyRequest.php b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php new file mode 100644 index 0000000000000..bcbfc43d567a9 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php @@ -0,0 +1,73 @@ +dataObjectFactory = $dataObjectFactory; + } + + /** + * Returns buy request for working with cart items + * + * @param float $qty + * @param array $customizableOptionsData + * @return DataObject + */ + public function execute(float $qty, array $customizableOptionsData): DataObject + { + $customizableOptions = []; + foreach ($customizableOptionsData as $customizableOption) { + if (isset($customizableOption['value_string'])) { + $customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue( + $customizableOption['value_string'] + ); + } + } + + return $this->dataObjectFactory->create([ + 'data' => [ + 'qty' => $qty, + 'options' => $customizableOptions, + ], + ]); + } + + /** + * Convert custom options value + * + * @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/Model/Cart/UpdateCartItem.php b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php index 5d166ec882875..7d653788ab0af 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php @@ -7,8 +7,6 @@ namespace Magento\QuoteGraphQl\Model\Cart; -use Magento\Framework\DataObject; -use Magento\Framework\DataObjectFactory; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -24,6 +22,11 @@ */ class UpdateCartItem { + /** + * @var CreateBuyRequest + */ + private $createBuyRequest; + /** * @var CartRepositoryInterface */ @@ -35,23 +38,18 @@ class UpdateCartItem private $cartItemRepository; /** - * @var DataObjectFactory - */ - private $dataObjectFactory; - - /** - * @param DataObjectFactory $dataObjectFactory * @param CartItemRepositoryInterface $cartItemRepository * @param CartRepositoryInterface $quoteRepository + * @param CreateBuyRequest $createBuyRequest */ public function __construct( - DataObjectFactory $dataObjectFactory, CartItemRepositoryInterface $cartItemRepository, - CartRepositoryInterface $quoteRepository + CartRepositoryInterface $quoteRepository, + CreateBuyRequest $createBuyRequest ) { - $this->dataObjectFactory = $dataObjectFactory; $this->cartItemRepository = $cartItemRepository; $this->quoteRepository = $quoteRepository; + $this->createBuyRequest = $createBuyRequest; } /** @@ -59,32 +57,25 @@ public function __construct( * * @param Quote $cart * @param int $cartItemId - * @param float $qty + * @param float $quantity * @param array $customizableOptionsData * @return void * @throws GraphQlInputException * @throws GraphQlNoSuchEntityException * @throws NoSuchEntityException */ - public function execute(Quote $cart, int $cartItemId, float $qty, array $customizableOptionsData): void + public function execute(Quote $cart, int $cartItemId, float $quantity, array $customizableOptionsData): void { if (count($customizableOptionsData) === 0) { // Update only item's qty - $this->updateItemQty($cartItemId, $cart, $qty); + $this->updateItemQuantity($cartItemId, $cart, $quantity); return; } - $customizableOptions = []; - foreach ($customizableOptionsData as $customizableOption) { - $customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue( - $customizableOption['value_string'] - ); - } - try { $result = $cart->updateItem( $cartItemId, - $this->createBuyRequest($qty, $customizableOptions) + $this->createBuyRequest->execute($quantity, $customizableOptionsData) ); } catch (LocalizedException $e) { throw new GraphQlInputException( @@ -117,12 +108,12 @@ public function execute(Quote $cart, int $cartItemId, float $qty, array $customi * * @param int $itemId * @param Quote $cart - * @param float $qty + * @param float $quantity * @throws GraphQlNoSuchEntityException * @throws NoSuchEntityException * @throws GraphQlNoSuchEntityException */ - private function updateItemQty(int $itemId, Quote $cart, float $qty) + private function updateItemQuantity(int $itemId, Quote $cart, float $quantity) { $cartItem = $cart->getItemById($itemId); if ($cartItem === false) { @@ -130,7 +121,7 @@ private function updateItemQty(int $itemId, Quote $cart, float $qty) __('Could not find cart item with id: %1.', $itemId) ); } - $cartItem->setQty($qty); + $cartItem->setQty($quantity); $this->validateCartItem($cartItem); $this->cartItemRepository->save($cartItem); } @@ -159,40 +150,4 @@ private function validateCartItem(Item $cartItem): void } } } - - /** - * Format GraphQl input data to a shape that buy request has - * - * @param float $qty - * @param array $customOptions - * @return DataObject - */ - private function createBuyRequest(float $qty, array $customOptions): DataObject - { - return $this->dataObjectFactory->create([ - 'data' => [ - 'qty' => $qty, - 'options' => $customOptions, - ], - ]); - } - - // TODO: Refactor the code duplication with addCartItem - // TODO: Make a reusable logic that is shared between add to cart / change cart approaches - - /** - * 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/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php similarity index 99% rename from dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php rename to dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php index 1aaa23860f5f1..8c46f66e70cf0 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php @@ -19,7 +19,7 @@ /** * Edit cart customizable options test */ -class EditQuoteItemCustomOptionsTest extends GraphQlAbstract +class EditQuoteItemWithCustomOptionsTest extends GraphQlAbstract { /** * @var ProductRepositoryInterface From d3734f85853a8ed77f4d6255058f4c8f042f2126 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 24 Jun 2019 15:46:50 -0500 Subject: [PATCH 8/8] magento/graphql-ce#622: No possibility to update customizable_options in updateCartItems mutation - code style + minor fixes --- .../Model/Cart/AddSimpleProductToCart.php | 23 ------------------- .../Model/Cart/CreateBuyRequest.php | 14 ++++++----- .../Model/Cart/UpdateCartItem.php | 18 +++++---------- .../EditQuoteItemWithCustomOptionsTest.php | 1 - .../add_simple_product_with_options.php | 14 ++++++----- 5 files changed, 22 insertions(+), 48 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index 0bf7968a50fae..3f6cc42614030 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -114,27 +114,4 @@ private function extractQuantity(array $cartItemData): float } return $quantity; } - - /** - * Extract Customizable Options from cart item data - * - * @param array $cartItemData - * @return array - */ - private function extractCustomizableOptions(array $cartItemData): array - { - if (!isset($cartItemData['customizable_options']) || empty($cartItemData['customizable_options'])) { - return []; - } - - $customizableOptionsData = []; - foreach ($cartItemData['customizable_options'] as $customizableOption) { - if (isset($customizableOption['value_string'])) { - $customizableOptionsData[$customizableOption['id']] = $this->convertCustomOptionValue( - $customizableOption['value_string'] - ); - } - } - return $customizableOptionsData; - } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php index bcbfc43d567a9..b95be8db2dccd 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php @@ -47,12 +47,14 @@ public function execute(float $qty, array $customizableOptionsData): DataObject } } - return $this->dataObjectFactory->create([ - 'data' => [ - 'qty' => $qty, - 'options' => $customizableOptions, - ], - ]); + return $this->dataObjectFactory->create( + [ + 'data' => [ + 'qty' => $qty, + 'options' => $customizableOptions, + ], + ] + ); } /** diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php index 7d653788ab0af..b18c6ad662335 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.php @@ -18,7 +18,6 @@ /** * Update cart item - * */ class UpdateCartItem { @@ -86,18 +85,13 @@ public function execute(Quote $cart, int $cartItemId, float $quantity, array $cu ); } - if (is_string($result)) { - throw new GraphQlInputException(__( - 'Could not update cart item: %message', - ['message' => $result] - )); - } - if ($result->getHasError()) { - throw new GraphQlInputException(__( - 'Could not update cart item: %message', - ['message' => $result->getMessage(true)] - )); + throw new GraphQlInputException( + __( + 'Could not update cart item: %message', + ['message' => $result->getMessage()] + ) + ); } $this->quoteRepository->save($cart); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php index 8c46f66e70cf0..62c1ae0dab3c7 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php @@ -240,7 +240,6 @@ private function getQuoteItemIdBySku(string $sku): int return (int)$quoteItem->getId(); } - /** * Generate an array with test values for customizable options * based on the option type diff --git a/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php index 68e2261ab02b0..ecd1428a5d617 100644 --- a/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php +++ b/dev/tests/integration/testsuite/Magento/GraphQl/Quote/_files/add_simple_product_with_options.php @@ -38,12 +38,14 @@ $cartItemCustomOptions[$productOption->getId()] = 'initial value'; } -$request = $dataObjectFactory->create([ - 'data' => [ - 'qty' => 1.0, - 'options' => $cartItemCustomOptions, - ], -]); +$request = $dataObjectFactory->create( + [ + 'data' => [ + 'qty' => 1.0, + 'options' => $cartItemCustomOptions, + ], + ] +); $quote = $quoteFactory->create(); $quoteResource->load($quote, 'test_quote', 'reserved_order_id');