Skip to content

Commit 7002ebc

Browse files
authored
Merge pull request #4009 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents bb46a2f + 29a039d commit 7002ebc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+988
-209
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
10+
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1111
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1315
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1416
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;
1517

@@ -23,13 +25,21 @@ class QuoteAddressFactory
2325
*/
2426
private $quoteAddressFactory;
2527

28+
/**
29+
* @var GetCustomerAddress
30+
*/
31+
private $getCustomerAddress;
32+
2633
/**
2734
* @param BaseQuoteAddressFactory $quoteAddressFactory
35+
* @param GetCustomerAddress $getCustomerAddress
2836
*/
2937
public function __construct(
30-
BaseQuoteAddressFactory $quoteAddressFactory
38+
BaseQuoteAddressFactory $quoteAddressFactory,
39+
GetCustomerAddress $getCustomerAddress
3140
) {
3241
$this->quoteAddressFactory = $quoteAddressFactory;
42+
$this->getCustomerAddress = $getCustomerAddress;
3343
}
3444

3545
/**
@@ -48,14 +58,19 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
4858
}
4959

5060
/**
51-
* Create QuoteAddress based on CustomerAddress
61+
* Create Quote Address based on Customer Address
5262
*
53-
* @param CustomerAddress $customerAddress
63+
* @param int $customerAddressId
64+
* @param int $customerId
5465
* @return QuoteAddress
5566
* @throws GraphQlInputException
67+
* @throws GraphQlAuthorizationException
68+
* @throws GraphQlNoSuchEntityException
5669
*/
57-
public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress
70+
public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress
5871
{
72+
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, $customerId);
73+
5974
$quoteAddress = $this->quoteAddressFactory->create();
6075
try {
6176
$quoteAddress->importCustomerAddressData($customerAddress);

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1110
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1314
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1415
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -29,11 +30,6 @@ class SetBillingAddressOnCart
2930
*/
3031
private $getCustomer;
3132

32-
/**
33-
* @var GetCustomerAddress
34-
*/
35-
private $getCustomerAddress;
36-
3733
/**
3834
* @var AssignBillingAddressToCart
3935
*/
@@ -42,18 +38,15 @@ class SetBillingAddressOnCart
4238
/**
4339
* @param QuoteAddressFactory $quoteAddressFactory
4440
* @param GetCustomer $getCustomer
45-
* @param GetCustomerAddress $getCustomerAddress
4641
* @param AssignBillingAddressToCart $assignBillingAddressToCart
4742
*/
4843
public function __construct(
4944
QuoteAddressFactory $quoteAddressFactory,
5045
GetCustomer $getCustomer,
51-
GetCustomerAddress $getCustomerAddress,
5246
AssignBillingAddressToCart $assignBillingAddressToCart
5347
) {
5448
$this->quoteAddressFactory = $quoteAddressFactory;
5549
$this->getCustomer = $getCustomer;
56-
$this->getCustomerAddress = $getCustomerAddress;
5750
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
5851
}
5952

@@ -65,6 +58,8 @@ public function __construct(
6558
* @param array $billingAddressInput
6659
* @return void
6760
* @throws GraphQlInputException
61+
* @throws GraphQlAuthenticationException
62+
* @throws GraphQlAuthorizationException
6863
* @throws GraphQlNoSuchEntityException
6964
*/
7065
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
@@ -97,8 +92,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
9792
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
9893
} else {
9994
$customer = $this->getCustomer->execute($context);
100-
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());
101-
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
95+
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
96+
(int)$customerAddressId,
97+
(int)$customer->getId()
98+
);
10299
}
103100

104101
$this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping);

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

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

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1110
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
1211
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1312
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -28,11 +27,6 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
2827
*/
2928
private $getCustomer;
3029

31-
/**
32-
* @var GetCustomerAddress
33-
*/
34-
private $getCustomerAddress;
35-
3630
/**
3731
* @var AssignShippingAddressToCart
3832
*/
@@ -41,18 +35,15 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
4135
/**
4236
* @param QuoteAddressFactory $quoteAddressFactory
4337
* @param GetCustomer $getCustomer
44-
* @param GetCustomerAddress $getCustomerAddress
4538
* @param AssignShippingAddressToCart $assignShippingAddressToCart
4639
*/
4740
public function __construct(
4841
QuoteAddressFactory $quoteAddressFactory,
4942
GetCustomer $getCustomer,
50-
GetCustomerAddress $getCustomerAddress,
5143
AssignShippingAddressToCart $assignShippingAddressToCart
5244
) {
5345
$this->quoteAddressFactory = $quoteAddressFactory;
5446
$this->getCustomer = $getCustomer;
55-
$this->getCustomerAddress = $getCustomerAddress;
5647
$this->assignShippingAddressToCart = $assignShippingAddressToCart;
5748
}
5849

@@ -86,8 +77,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
8677
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
8778
} else {
8879
$customer = $this->getCustomer->execute($context);
89-
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());
90-
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
80+
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
81+
(int)$customerAddressId,
82+
(int)$customer->getId()
83+
);
9184
}
9285

9386
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Quote\Api\CouponManagementInterface;
1415

1516
/**
1617
* @inheritdoc
1718
*/
1819
class AppliedCoupon implements ResolverInterface
1920
{
21+
/**
22+
* @var CouponManagementInterface
23+
*/
24+
private $couponManagement;
25+
26+
/**
27+
* @param CouponManagementInterface $couponManagement
28+
*/
29+
public function __construct(
30+
CouponManagementInterface $couponManagement
31+
) {
32+
$this->couponManagement = $couponManagement;
33+
}
34+
2035
/**
2136
* @inheritdoc
2237
*/
@@ -26,9 +41,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
2641
throw new LocalizedException(__('"model" value should be specified'));
2742
}
2843
$cart = $value['model'];
44+
$cartId = $cart->getId();
2945

30-
$appliedCoupon = $cart->getCouponCode();
31-
46+
$appliedCoupon = $this->couponManagement->get($cartId);
3247
return $appliedCoupon ? ['code' => $appliedCoupon] : null;
3348
}
3449
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Quote\Api\CartManagementInterface;
18+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
19+
use Magento\Sales\Api\OrderRepositoryInterface;
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
class PlaceOrder implements ResolverInterface
25+
{
26+
/**
27+
* @var CartManagementInterface
28+
*/
29+
private $cartManagement;
30+
31+
/**
32+
* @var GetCartForUser
33+
*/
34+
private $getCartForUser;
35+
36+
/**
37+
* @var OrderRepositoryInterface
38+
*/
39+
private $orderRepository;
40+
41+
/**
42+
* @param GetCartForUser $getCartForUser
43+
* @param CartManagementInterface $cartManagement
44+
* @param OrderRepositoryInterface $orderRepository
45+
*/
46+
public function __construct(
47+
GetCartForUser $getCartForUser,
48+
CartManagementInterface $cartManagement,
49+
OrderRepositoryInterface $orderRepository
50+
) {
51+
$this->getCartForUser = $getCartForUser;
52+
$this->cartManagement = $cartManagement;
53+
$this->orderRepository = $orderRepository;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
60+
{
61+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
62+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
63+
}
64+
$maskedCartId = $args['input']['cart_id'];
65+
66+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
67+
68+
try {
69+
$orderId = $this->cartManagement->placeOrder($cart->getId());
70+
$order = $this->orderRepository->get($orderId);
71+
72+
return [
73+
'order' => [
74+
'order_id' => $order->getIncrementId(),
75+
],
76+
];
77+
} catch (NoSuchEntityException $e) {
78+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
79+
} catch (LocalizedException $e) {
80+
throw new GraphQlInputException(__('Unable to place order: %message', ['message' => $e->getMessage()]), $e);
81+
}
82+
}
83+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6262
try {
6363
$this->couponManagement->remove($cartId);
6464
} catch (NoSuchEntityException $e) {
65-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
65+
$message = $e->getMessage();
66+
if (preg_match('/The "\d+" Cart doesn\'t contain products/', $message)) {
67+
$message = 'Cart does not contain products';
68+
}
69+
throw new GraphQlNoSuchEntityException(__($message), $e);
6670
} catch (CouldNotDeleteException $e) {
6771
throw new LocalizedException(__($e->getMessage()), $e);
6872
}

app/code/Magento/QuoteGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"magento/module-catalog": "*",
1111
"magento/module-store": "*",
1212
"magento/module-customer": "*",
13-
"magento/module-customer-graph-ql": "*"
13+
"magento/module-customer-graph-ql": "*",
14+
"magento/module-sales": "*"
1415
},
1516
"suggest": {
1617
"magento/module-graph-ql": "*"

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

Lines changed: 13 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+
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
2021
}
2122

2223
input AddSimpleProductsToCartInput {
@@ -114,6 +115,10 @@ input ShippingMethodInput {
114115
method_code: String!
115116
}
116117

118+
input PlaceOrderInput {
119+
cart_id: String!
120+
}
121+
117122
input SetPaymentMethodOnCartInput {
118123
cart_id: String!
119124
payment_method: PaymentMethodInput!
@@ -144,6 +149,10 @@ type ApplyCouponToCartOutput {
144149
cart: Cart!
145150
}
146151

152+
type PlaceOrderOutput {
153+
order: Order!
154+
}
155+
147156
type Cart {
148157
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
149158
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon")
@@ -285,3 +294,7 @@ type CartItemSelectedOptionValuePrice {
285294
units: String!
286295
type: PriceTypeEnum!
287296
}
297+
298+
type Order {
299+
order_id: String
300+
}

0 commit comments

Comments
 (0)