Skip to content

Commit fd891aa

Browse files
Merge branch 2.3-develop into ENGCOM-4679-magento-magento2-22149
2 parents 7da6928 + 1ffd8c5 commit fd891aa

24 files changed

+1743
-77
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Quote\Model\Quote;
15+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
class CartEmail implements ResolverInterface
21+
{
22+
/**
23+
* @var GetCartForUser
24+
*/
25+
private $getCartForUser;
26+
27+
/**
28+
* @param GetCartForUser $getCartForUser
29+
*/
30+
public function __construct(
31+
GetCartForUser $getCartForUser
32+
) {
33+
$this->getCartForUser = $getCartForUser;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
40+
{
41+
if (!isset($value['model'])) {
42+
throw new LocalizedException(__('"model" value should be specified'));
43+
}
44+
/** @var Quote $cart */
45+
$cart = $value['model'];
46+
47+
return $cart->getCustomerEmail();
48+
}
49+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Quote\Model\Quote;
15+
use Magento\Quote\Model\Quote\Address\Total;
16+
use Magento\Quote\Model\Quote\TotalsCollector;
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
class CartPrices implements ResolverInterface
22+
{
23+
/**
24+
* @var TotalsCollector
25+
*/
26+
private $totalsCollector;
27+
28+
/**
29+
* @param TotalsCollector $totalsCollector
30+
*/
31+
public function __construct(
32+
TotalsCollector $totalsCollector
33+
) {
34+
$this->totalsCollector = $totalsCollector;
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
41+
{
42+
if (!isset($value['model'])) {
43+
throw new LocalizedException(__('"model" value should be specified'));
44+
}
45+
46+
/** @var Quote $quote */
47+
$quote = $value['model'];
48+
$cartTotals = $this->totalsCollector->collectQuoteTotals($quote);
49+
$currency = $quote->getQuoteCurrencyCode();
50+
51+
return [
52+
'grand_total' => ['value' => $cartTotals->getGrandTotal(), 'currency' => $currency],
53+
'subtotal_including_tax' => ['value' => $cartTotals->getSubtotalInclTax(), 'currency' => $currency],
54+
'subtotal_excluding_tax' => ['value' => $cartTotals->getSubtotal(), 'currency' => $currency],
55+
'subtotal_with_discount_excluding_tax' => [
56+
'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency
57+
],
58+
'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency),
59+
'model' => $quote
60+
];
61+
}
62+
63+
/**
64+
* Returns taxes applied to the current quote
65+
*
66+
* @param Total $total
67+
* @param string $currency
68+
* @return array
69+
*/
70+
private function getAppliedTaxes(Total $total, string $currency): array
71+
{
72+
$appliedTaxesData = [];
73+
$appliedTaxes = $total->getAppliedTaxes();
74+
75+
if (count($appliedTaxes) === 0) {
76+
return $appliedTaxesData;
77+
}
78+
79+
foreach ($appliedTaxes as $appliedTax) {
80+
$appliedTaxesData[] = [
81+
'label' => $appliedTax['id'],
82+
'amount' => ['value' => $appliedTax['amount'], 'currency' => $currency]
83+
];
84+
}
85+
return $appliedTaxesData;
86+
}
87+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6565

6666
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
6767

68+
if ($context->getUserId() === 0) {
69+
if (!$cart->getCustomerEmail()) {
70+
throw new GraphQlInputException(__("Guest email for cart is missing. Please enter"));
71+
}
72+
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
73+
}
74+
6875
try {
6976
$orderId = $this->cartManagement->placeOrder($cart->getId());
7077
$order = $this->orderRepository->get($orderId);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
17+
use Magento\Quote\Api\CartRepositoryInterface;
18+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
class SetGuestEmailOnCart implements ResolverInterface
24+
{
25+
/**
26+
* @var CartRepositoryInterface
27+
*/
28+
private $cartRepository;
29+
30+
/**
31+
* @var GetCartForUser
32+
*/
33+
private $getCartForUser;
34+
35+
/**
36+
* @var EmailAddressValidator
37+
*/
38+
private $emailValidator;
39+
40+
/**
41+
* @param GetCartForUser $getCartForUser
42+
* @param CartRepositoryInterface $cartRepository
43+
* @param EmailAddressValidator $emailValidator
44+
*/
45+
public function __construct(
46+
GetCartForUser $getCartForUser,
47+
CartRepositoryInterface $cartRepository,
48+
EmailAddressValidator $emailValidator
49+
) {
50+
$this->getCartForUser = $getCartForUser;
51+
$this->cartRepository = $cartRepository;
52+
$this->emailValidator = $emailValidator;
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
59+
{
60+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
61+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
62+
}
63+
$maskedCartId = $args['input']['cart_id'];
64+
65+
if (!isset($args['input']['email']) || empty($args['input']['email'])) {
66+
throw new GraphQlInputException(__('Required parameter "email" is missing'));
67+
}
68+
69+
if (false === $this->emailValidator->isValid($args['input']['email'])) {
70+
throw new GraphQlInputException(__('Invalid email format'));
71+
}
72+
$email = $args['input']['email'];
73+
74+
$currentUserId = $context->getUserId();
75+
76+
if ($currentUserId !== 0) {
77+
throw new GraphQlInputException(__('The request is not allowed for logged in customers'));
78+
}
79+
80+
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId);
81+
$cart->setCustomerEmail($email);
82+
83+
try {
84+
$this->cartRepository->save($cart);
85+
} catch (CouldNotSaveException $e) {
86+
throw new LocalizedException(__($e->getMessage()), $e);
87+
}
88+
89+
return [
90+
'cart' => [
91+
'model' => $cart,
92+
],
93+
];
94+
}
95+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Mutation {
1717
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart")
1818
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
1919
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
20+
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
2021
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
2122
}
2223

@@ -129,6 +130,24 @@ input PaymentMethodInput {
129130
purchase_order_number: String @doc(description:"Purchase order number")
130131
}
131132

133+
input SetGuestEmailOnCartInput {
134+
cart_id: String!
135+
email: String!
136+
}
137+
138+
type CartPrices {
139+
grand_total: Money
140+
subtotal_including_tax: Money
141+
subtotal_excluding_tax: Money
142+
subtotal_with_discount_excluding_tax: Money
143+
applied_taxes: [CartTaxItem]
144+
}
145+
146+
type CartTaxItem {
147+
amount: Money!
148+
label: String!
149+
}
150+
132151
type SetPaymentMethodOnCartOutput {
133152
cart: Cart!
134153
}
@@ -156,10 +175,12 @@ type PlaceOrderOutput {
156175
type Cart {
157176
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
158177
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon")
178+
email: String @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartEmail")
159179
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
160180
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
161181
available_payment_methods: [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
162182
selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod")
183+
prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPrices")
163184
}
164185

165186
type CartAddress {
@@ -258,6 +279,10 @@ type RemoveItemFromCartOutput {
258279
cart: Cart!
259280
}
260281

282+
type SetGuestEmailOnCartOutput {
283+
cart: Cart!
284+
}
285+
261286
type SimpleCartItem implements CartItemInterface @doc(description: "Simple Cart Item") {
262287
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
263288
}

0 commit comments

Comments
 (0)