Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 28757e4

Browse files
authored
Merge pull request #4151 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 8b3fd9d + 9359cca commit 28757e4

31 files changed

+985
-203
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ConfigurableProductOptionsValues @doc(description: "ConfigurableProductOpti
4141

4242
input AddConfigurableProductsToCartInput {
4343
cart_id: String!
44-
cartItems: [ConfigurableProductCartItemInput!]!
44+
cart_items: [ConfigurableProductCartItemInput!]!
4545
}
4646

4747
type AddConfigurableProductsToCartOutput {

app/code/Magento/CustomerGraphQl/Model/Resolver/GenerateCustomerToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function resolve(
4444
array $value = null,
4545
array $args = null
4646
) {
47-
if (!isset($args['email'])) {
47+
if (!isset($args['email']) || empty($args['email'])) {
4848
throw new GraphQlInputException(__('Specify the "email" value.'));
4949
}
5050

51-
if (!isset($args['password'])) {
51+
if (!isset($args['password']) || empty($args['password'])) {
5252
throw new GraphQlInputException(__('Specify the "password" value.'));
5353
}
5454

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/Model/Resolver/AddSimpleProductsToCart.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5252
}
5353
$maskedCartId = $args['input']['cart_id'];
5454

55-
if (!isset($args['input']['cartItems']) || empty($args['input']['cartItems'])
56-
|| !is_array($args['input']['cartItems'])
55+
if (!isset($args['input']['cart_items']) || empty($args['input']['cart_items'])
56+
|| !is_array($args['input']['cart_items'])
5757
) {
58-
throw new GraphQlInputException(__('Required parameter "cartItems" is missing'));
58+
throw new GraphQlInputException(__('Required parameter "cart_items" is missing'));
5959
}
60-
$cartItems = $args['input']['cartItems'];
60+
$cartItems = $args['input']['cart_items'];
6161

6262
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
6363
$this->addProductsToCart->execute($cart, $cartItems);

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/AvailableShippingMethods.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;
99

10+
use Magento\Directory\Model\Currency;
1011
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1112
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
1315
use Magento\Framework\GraphQl\Query\ResolverInterface;
1416
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1517
use Magento\Quote\Api\Data\ShippingMethodInterface;
1618
use Magento\Quote\Model\Cart\ShippingMethodConverter;
19+
use Magento\Store\Model\StoreManagerInterface;
1720

1821
/**
1922
* @inheritdoc
@@ -30,16 +33,24 @@ class AvailableShippingMethods implements ResolverInterface
3033
*/
3134
private $shippingMethodConverter;
3235

36+
/**
37+
* @var StoreManagerInterface
38+
*/
39+
private $storeManager;
40+
3341
/**
3442
* @param ExtensibleDataObjectConverter $dataObjectConverter
3543
* @param ShippingMethodConverter $shippingMethodConverter
44+
* @param StoreManagerInterface $storeManager
3645
*/
3746
public function __construct(
3847
ExtensibleDataObjectConverter $dataObjectConverter,
39-
ShippingMethodConverter $shippingMethodConverter
48+
ShippingMethodConverter $shippingMethodConverter,
49+
StoreManagerInterface $storeManager
4050
) {
4151
$this->dataObjectConverter = $dataObjectConverter;
4252
$this->shippingMethodConverter = $shippingMethodConverter;
53+
$this->storeManager = $storeManager;
4354
}
4455

4556
/**
@@ -65,13 +76,44 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6576
$shippingRates = $address->getGroupedAllShippingRates();
6677
foreach ($shippingRates as $carrierRates) {
6778
foreach ($carrierRates as $rate) {
68-
$methods[] = $this->dataObjectConverter->toFlatArray(
79+
$methodData = $this->dataObjectConverter->toFlatArray(
6980
$this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode()),
7081
[],
7182
ShippingMethodInterface::class
7283
);
84+
$methods[] = $this->processMoneyTypeData($methodData, $cart->getQuoteCurrencyCode());
7385
}
7486
}
7587
return $methods;
7688
}
89+
90+
/**
91+
* Process money type data
92+
*
93+
* @param array $data
94+
* @param string $quoteCurrencyCode
95+
* @return array
96+
* @throws NoSuchEntityException
97+
*/
98+
private function processMoneyTypeData(array $data, string $quoteCurrencyCode): array
99+
{
100+
if (isset($data['amount'])) {
101+
$data['amount'] = ['value' => $data['amount'], 'currency' => $quoteCurrencyCode];
102+
}
103+
104+
if (isset($data['base_amount'])) {
105+
/** @var Currency $currency */
106+
$currency = $this->storeManager->getStore()->getBaseCurrency();
107+
$data['base_amount'] = ['value' => $data['base_amount'], 'currency' => $currency->getCode()];
108+
}
109+
110+
if (isset($data['price_excl_tax'])) {
111+
$data['price_excl_tax'] = ['value' => $data['price_excl_tax'], 'currency' => $quoteCurrencyCode];
112+
}
113+
114+
if (isset($data['price_incl_tax'])) {
115+
$data['price_incl_tax'] = ['value' => $data['price_incl_tax'], 'currency' => $quoteCurrencyCode];
116+
}
117+
return $data;
118+
}
77119
}

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,34 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;
99

10+
use Magento\Directory\Model\Currency;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
1314
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1415
use Magento\Quote\Model\Quote\Address;
1516
use Magento\Quote\Model\Quote\Address\Rate;
17+
use Magento\Store\Model\StoreManagerInterface;
1618

1719
/**
1820
* @inheritdoc
1921
*/
2022
class SelectedShippingMethod implements ResolverInterface
2123
{
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @param StoreManagerInterface $storeManager
31+
*/
32+
public function __construct(
33+
StoreManagerInterface $storeManager
34+
) {
35+
$this->storeManager = $storeManager;
36+
}
37+
2238
/**
2339
* @inheritdoc
2440
*/
@@ -30,32 +46,44 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
3046
/** @var Address $address */
3147
$address = $value['model'];
3248
$rates = $address->getAllShippingRates();
49+
$carrierTitle = null;
50+
$methodTitle = null;
3351

3452
if (count($rates) > 0) {
3553
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
54+
3655
/** @var Rate $rate */
37-
$rate = current($rates);
56+
foreach ($rates as $rate) {
57+
if ($rate->getCode() == $address->getShippingMethod()) {
58+
$carrierTitle = $rate->getCarrierTitle();
59+
$methodTitle = $rate->getMethodTitle();
60+
break;
61+
}
62+
}
63+
64+
/** @var Currency $currency */
65+
$currency = $this->storeManager->getStore()->getBaseCurrency();
3866

3967
$data = [
4068
'carrier_code' => $carrierCode,
4169
'method_code' => $methodCode,
42-
'carrier_title' => $rate->getCarrierTitle(),
43-
'method_title' => $rate->getMethodTitle(),
70+
'carrier_title' => $carrierTitle,
71+
'method_title' => $methodTitle,
4472
'amount' => [
4573
'value' => $address->getShippingAmount(),
4674
'currency' => $address->getQuote()->getQuoteCurrencyCode(),
4775
],
4876
'base_amount' => [
4977
'value' => $address->getBaseShippingAmount(),
50-
'currency' => $address->getQuote()->getBaseCurrencyCode(),
78+
'currency' => $currency->getCode(),
5179
],
5280
];
5381
} else {
5482
$data = [
5583
'carrier_code' => null,
5684
'method_code' => null,
57-
'carrier_title' => null,
58-
'method_title' => null,
85+
'carrier_title' => $carrierTitle,
86+
'method_title' => $methodTitle,
5987
'amount' => null,
6088
'base_amount' => null,
6189
];

app/code/Magento/QuoteGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"magento/module-store": "*",
1212
"magento/module-customer": "*",
1313
"magento/module-customer-graph-ql": "*",
14-
"magento/module-sales": "*"
14+
"magento/module-sales": "*",
15+
"magento/module-directory": "*"
1516
},
1617
"suggest": {
1718
"magento/module-graph-ql": "*",

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>

0 commit comments

Comments
 (0)