Skip to content

Commit 3ccf676

Browse files
authored
Merge pull request #4115 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents d1906d8 + 5899e77 commit 3ccf676

18 files changed

+507
-369
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function execute(QuoteAddress $address): array
5050
}
5151

5252
$addressData = array_merge($addressData, [
53-
'address_id' => $address->getId(),
5453
'address_type' => $addressType,
5554
'country' => [
5655
'code' => $address->getCountryId(),

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

Lines changed: 0 additions & 83 deletions
This file was deleted.

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,17 @@
1616
*/
1717
class SetShippingMethodsOnCart implements SetShippingMethodsOnCartInterface
1818
{
19-
/**
20-
* @var GetQuoteAddress
21-
*/
22-
private $getQuoteAddress;
23-
2419
/**
2520
* @var AssignShippingMethodToCart
2621
*/
2722
private $assignShippingMethodToCart;
2823

2924
/**
30-
* @param GetQuoteAddress $getQuoteAddress
3125
* @param AssignShippingMethodToCart $assignShippingMethodToCart
3226
*/
3327
public function __construct(
34-
GetQuoteAddress $getQuoteAddress,
3528
AssignShippingMethodToCart $assignShippingMethodToCart
3629
) {
37-
$this->getQuoteAddress = $getQuoteAddress;
3830
$this->assignShippingMethodToCart = $assignShippingMethodToCart;
3931
}
4032

@@ -50,11 +42,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
5042
}
5143
$shippingMethodInput = current($shippingMethodsInput);
5244

53-
if (!isset($shippingMethodInput['cart_address_id']) || empty($shippingMethodInput['cart_address_id'])) {
54-
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing.'));
55-
}
56-
$cartAddressId = $shippingMethodInput['cart_address_id'];
57-
5845
if (!isset($shippingMethodInput['carrier_code']) || empty($shippingMethodInput['carrier_code'])) {
5946
throw new GraphQlInputException(__('Required parameter "carrier_code" is missing.'));
6047
}
@@ -65,7 +52,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
6552
}
6653
$methodCode = $shippingMethodInput['method_code'];
6754

68-
$quoteAddress = $this->getQuoteAddress->execute($cart, $cartAddressId, $context->getUserId());
69-
$this->assignShippingMethodToCart->execute($cart, $quoteAddress, $carrierCode, $methodCode);
55+
$shippingAddress = $cart->getShippingAddress();
56+
$this->assignShippingMethodToCart->execute($cart, $shippingAddress, $carrierCode, $methodCode);
7057
}
7158
}

app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,15 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
}
7070
$paymentMethodCode = $args['input']['payment_method']['code'];
7171

72-
$poNumber = isset($args['input']['payment_method']['purchase_order_number'])
73-
&& empty($args['input']['payment_method']['purchase_order_number'])
74-
? $args['input']['payment_method']['purchase_order_number']
75-
: null;
72+
$poNumber = $args['input']['payment_method']['purchase_order_number'] ?? null;
73+
$additionalData = $args['input']['payment_method']['additional_data'] ?? [];
7674

7775
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
7876
$payment = $this->paymentFactory->create([
7977
'data' => [
8078
PaymentInterface::KEY_METHOD => $paymentMethodCode,
8179
PaymentInterface::KEY_PO_NUMBER => $poNumber,
82-
PaymentInterface::KEY_ADDITIONAL_DATA => [],
80+
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
8381
]
8482
]);
8583

app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1717
use Magento\Quote\Api\CartItemRepositoryInterface;
1818
use Magento\Quote\Model\Quote;
19+
use Magento\Quote\Model\Quote\Item;
1920
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
2021

2122
/**
@@ -111,8 +112,35 @@ private function processCartItems(Quote $cart, array $items): void
111112
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
112113
} else {
113114
$cartItem->setQty($qty);
115+
$this->validateCartItem($cartItem);
114116
$this->cartItemRepository->save($cartItem);
115117
}
116118
}
117119
}
120+
121+
/**
122+
* Validate cart item
123+
*
124+
* @param Item $cartItem
125+
* @return void
126+
* @throws GraphQlInputException
127+
*/
128+
private function validateCartItem(Item $cartItem): void
129+
{
130+
if ($cartItem->getHasError()) {
131+
$errors = [];
132+
foreach ($cartItem->getMessage(false) as $message) {
133+
$errors[] = $message;
134+
}
135+
136+
if (!empty($errors)) {
137+
throw new GraphQlInputException(
138+
__(
139+
'Could not update the product with SKU %sku: %message',
140+
['sku' => $cartItem->getSku(), 'message' => __(implode("\n", $errors))]
141+
)
142+
);
143+
}
144+
}
145+
}
118146
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ input SetShippingMethodsOnCartInput {
115115
}
116116

117117
input ShippingMethodInput {
118-
cart_address_id: Int!
119118
carrier_code: String!
120119
method_code: String!
121120
}
@@ -131,9 +130,13 @@ input SetPaymentMethodOnCartInput {
131130

132131
input PaymentMethodInput {
133132
code: String! @doc(description:"Payment method code")
133+
additional_data: PaymentMethodAdditionalDataInput @doc(description:"Additional payment data")
134134
purchase_order_number: String @doc(description:"Purchase order number")
135135
}
136136

137+
input PaymentMethodAdditionalDataInput {
138+
}
139+
137140
input SetGuestEmailOnCartInput {
138141
cart_id: String!
139142
email: String!
@@ -188,7 +191,6 @@ type Cart {
188191
}
189192

190193
type CartAddress {
191-
address_id: Int
192194
firstname: String
193195
lastname: String
194196
company: String
@@ -231,13 +233,14 @@ type SelectedShippingMethod {
231233
type AvailableShippingMethod {
232234
carrier_code: String!
233235
carrier_title: String!
234-
method_code: String!
235-
method_title: String!
236+
method_code: String @doc(description: "Could be null if method is not available")
237+
method_title: String @doc(description: "Could be null if method is not available")
236238
error_message: String
237239
amount: Float!
238-
base_amount: Float!
240+
base_amount: Float @doc(description: "Could be null if method is not available")
239241
price_excl_tax: Float!
240242
price_incl_tax: Float!
243+
available: Boolean!
241244
}
242245

243246
type AvailablePaymentMethod {
@@ -247,9 +250,13 @@ type AvailablePaymentMethod {
247250

248251
type SelectedPaymentMethod {
249252
code: String @doc(description: "The payment method code")
253+
additional_data: SelectedPaymentMethodAdditionalData @doc(description: "Additional payment data")
250254
purchase_order_number: String @doc(description: "The purchase order number.")
251255
}
252256

257+
type SelectedPaymentMethodAdditionalData {
258+
}
259+
253260
enum AdressTypeEnum {
254261
SHIPPING
255262
BILLING

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,31 @@ public function testAddSimpleProductToCartWithNegativeQty()
8181
$this->graphQlMutation($query);
8282
}
8383

84+
/**
85+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
86+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
87+
*/
88+
public function testAddProductIfQuantityIsDecimal()
89+
{
90+
$sku = 'simple_product';
91+
$qty = 0.2;
92+
93+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
94+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
95+
96+
$this->expectExceptionMessage(
97+
"Could not add the product with SKU {$sku} to the shopping cart: The fewest you may purchase is 1"
98+
);
99+
$this->graphQlMutation($query);
100+
}
101+
84102
/**
85103
* @param string $maskedQuoteId
86104
* @param string $sku
87-
* @param int $qty
105+
* @param float $qty
88106
* @return string
89107
*/
90-
private function getQuery(string $maskedQuoteId, string $sku, int $qty) : string
108+
private function getQuery(string $maskedQuoteId, string $sku, float $qty) : string
91109
{
92110
return <<<QUERY
93111
mutation {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\CatalogInventory;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test for updating/removing shopping cart items
17+
*/
18+
class UpdateCartItemsTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var GetMaskedQuoteIdByReservedOrderId
22+
*/
23+
private $getMaskedQuoteIdByReservedOrderId;
24+
25+
/**
26+
* @var GetQuoteItemIdByReservedQuoteIdAndSku
27+
*/
28+
private $getQuoteItemIdByReservedQuoteIdAndSku;
29+
30+
protected function setUp()
31+
{
32+
$objectManager = Bootstrap::getObjectManager();
33+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
34+
$this->getQuoteItemIdByReservedQuoteIdAndSku = $objectManager->get(
35+
GetQuoteItemIdByReservedQuoteIdAndSku::class
36+
);
37+
}
38+
39+
/**
40+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
41+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
42+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
43+
*/
44+
public function testUpdateCartItemDecimalQty()
45+
{
46+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
47+
$itemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');
48+
49+
$qty = 0.5;
50+
$this->expectExceptionMessage(
51+
"Could not update the product with SKU simple_product: The fewest you may purchase is 1"
52+
);
53+
$query = $this->getQuery($maskedQuoteId, $itemId, $qty);
54+
$this->graphQlMutation($query);
55+
}
56+
57+
/**
58+
* @param string $maskedQuoteId
59+
* @param int $itemId
60+
* @param float $qty
61+
* @return string
62+
*/
63+
private function getQuery(string $maskedQuoteId, int $itemId, float $qty): string
64+
{
65+
return <<<QUERY
66+
mutation {
67+
updateCartItems(input: {
68+
cart_id: "{$maskedQuoteId}"
69+
cart_items:[
70+
{
71+
cart_item_id: {$itemId}
72+
quantity: {$qty}
73+
}
74+
]
75+
}) {
76+
cart {
77+
items {
78+
id
79+
qty
80+
}
81+
}
82+
}
83+
}
84+
QUERY;
85+
}
86+
}

0 commit comments

Comments
 (0)