Skip to content

Commit 4694c06

Browse files
author
Valeriy Nayda
committed
GraphQL-39: Manage Shipping methods on Cart
1 parent 313544e commit 4694c06

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

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

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

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Authorization\Model\UserContextInterface;
1110
use Magento\Customer\Api\Data\AddressInterface;
12-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
11+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
1312
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1413
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1514
use Magento\Quote\Api\Data\CartInterface;
@@ -37,19 +36,27 @@ class SetShippingAddressOnCart implements SetShippingAddressesOnCartInterface
3736
*/
3837
private $addressModel;
3938

39+
/**
40+
* @var CheckCustomerAccount
41+
*/
42+
private $checkCustomerAccount;
43+
4044
/**
4145
* @param ShippingAddressManagementInterface $shippingAddressManagement
4246
* @param AddressRepositoryInterface $addressRepository
4347
* @param Address $addressModel
48+
* @param CheckCustomerAccount $checkCustomerAccount
4449
*/
4550
public function __construct(
4651
ShippingAddressManagementInterface $shippingAddressManagement,
4752
AddressRepositoryInterface $addressRepository,
48-
Address $addressModel
53+
Address $addressModel,
54+
CheckCustomerAccount $checkCustomerAccount
4955
) {
5056
$this->shippingAddressManagement = $shippingAddressManagement;
5157
$this->addressRepository = $addressRepository;
5258
$this->addressModel = $addressModel;
59+
$this->checkCustomerAccount = $checkCustomerAccount;
5360
}
5461

5562
/**
@@ -66,7 +73,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
6673
$customerAddressId = $shippingAddress['customer_address_id'] ?? null;
6774
$addressInput = $shippingAddress['address'] ?? null;
6875

69-
if (!$customerAddressId && !$addressInput) {
76+
if (null === $customerAddressId && null === $addressInput) {
7077
throw new GraphQlInputException(
7178
__('The shipping address must contain either "customer_address_id" or "address".')
7279
);
@@ -76,19 +83,14 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
7683
__('The shipping address cannot contain "customer_address_id" and "address" at the same time.')
7784
);
7885
}
79-
if ($customerAddressId) {
80-
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
81-
throw new GraphQlAuthorizationException(
82-
__(
83-
'Guest users cannot manage addresses.'
84-
)
85-
);
86-
}
86+
if (null === $customerAddressId) {
87+
$shippingAddress = $this->addressModel->addData($addressInput);
88+
} else {
89+
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
90+
8791
/** @var AddressInterface $customerAddress */
8892
$customerAddress = $this->addressRepository->getById($customerAddressId);
8993
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
90-
} else {
91-
$shippingAddress = $this->addressModel->addData($addressInput);
9294
}
9395

9496
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
* Extension point for setting shipping addresses for a specified shopping cart
1616
*
1717
* All objects that are responsible for setting shipping addresses on a cart via GraphQl
18-
*should implement this interface.
18+
* should implement this interface.
1919
*/
2020
interface SetShippingAddressesOnCartInterface
2121
{
22-
2322
/**
2423
* Set shipping addresses for a specified shopping cart
2524
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9393
return [
9494
'cart' => [
9595
'cart_id' => $maskedCartId,
96-
'model' => $cart
96+
'model' => $cart,
9797
]
9898
];
9999
}

app/code/Magento/QuoteGraphQl/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"magento/module-checkout": "*",
1010
"magento/module-catalog": "*",
1111
"magento/module-store": "*",
12-
"magento/module-customer": "*",
13-
"magento/module-authorization": "*"
12+
"magento/module-customer": "*"
1413
},
1514
"suggest": {
1615
"magento/module-graph-ql": "*"

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function testSetSavedShippingAddressOnCartByGuest()
139139
}
140140
}
141141
QUERY;
142-
self::expectExceptionMessage('Guest users cannot manage addresses.');
142+
self::expectExceptionMessage('The current customer isn\'t authorized.');
143143
$this->graphQlQuery($query);
144144
}
145145

0 commit comments

Comments
 (0)