Skip to content

Commit a0bb0fc

Browse files
author
vitaliyboyko
committed
graphQl: updated shipping address coverage content
1 parent ef8f126 commit a0bb0fc

File tree

5 files changed

+157
-38
lines changed

5 files changed

+157
-38
lines changed
Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,76 @@
11
<?php
22
/**
3-
* @author Atwix Team
4-
* @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
77

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

10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Multishipping\Model\Checkout\Type\Multishipping as MultishippingModel;
15+
use Magento\QuoteGraphQl\Model\Resolver\ShippingAddress\SetShippingAddressOnCart\MultiShipping\ShippingItemsMapper;
16+
1017
class MultiShipping
1118
{
1219
/**
20+
* @var MultishippingModel
21+
*/
22+
private $multishippingModel;
23+
24+
/**
25+
* @var ShippingItemsMapper
26+
*/
27+
private $shippingItemsInformationMapper;
28+
29+
/**
30+
* @param MultishippingModel $multishippingModel
31+
* @param ShippingItemsMapper $shippingItemsInformationMapper
32+
*/
33+
public function __construct(
34+
MultishippingModel $multishippingModel,
35+
ShippingItemsMapper $shippingItemsInformationMapper
36+
) {
37+
$this->multishippingModel = $multishippingModel;
38+
$this->shippingItemsInformationMapper = $shippingItemsInformationMapper;
39+
}
40+
41+
/**
42+
* @param ContextInterface $context
1343
* @param int $cartId
14-
* @param array $cartItems
15-
* @param int|null $customerAddressId
16-
* @param array|null $address
17-
* @return void
44+
* @param array $shippingAddresses
1845
*/
19-
public function setAddress(int $cartId, array $cartItems, ?int $customerAddressId, ?array $address): void
46+
public function setAddresses(ContextInterface $context, int $cartId, array $shippingAddresses): void
2047
{
21-
//TODO: implement multi shipping
48+
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
49+
throw new GraphQlAuthorizationException(
50+
__(
51+
'Multishipping allowed only for authorized customers'
52+
)
53+
);
54+
}
55+
56+
$shippingItemsInformation = [];
57+
foreach ($shippingAddresses as $shippingAddress) {
58+
$customerAddressId = $shippingAddress['customer_address_id'] ?? null;
59+
$cartItems = $shippingAddress['cart_items'] ?? null;
60+
if (!$customerAddressId) {
61+
throw new GraphQlInputException(__('Parameter "customer_address_id" is required for multishipping'));
62+
}
63+
if (!$cartItems) {
64+
throw new GraphQlInputException(__('Parameter "cart_items" is required for multishipping'));
65+
}
66+
67+
$shippingItemsInformation = array_merge(
68+
$shippingItemsInformation,
69+
$this->shippingItemsInformationMapper->map($shippingAddress)
70+
);
71+
}
72+
73+
//TODO: multishipping model works with session. Do we need to avoid it?
74+
$this->multishippingModel->setShippingItemsInformation($shippingItemsInformation);
2275
}
2376
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\ShippingAddress\SetShippingAddressOnCart\MultiShipping;
9+
10+
/**
11+
* Shipping address to shipping items mapper
12+
*/
13+
class ShippingItemsMapper
14+
{
15+
16+
/**
17+
* Converts shipping address input array into shipping items information array
18+
* Array structure:
19+
* array(
20+
* $cartItemId => array(
21+
* 'qty' => $qty,
22+
* 'address' => $customerAddressId
23+
* )
24+
* )
25+
*
26+
* @param array $shippingAddress
27+
* @return array
28+
*/
29+
public function map(array $shippingAddress): array
30+
{
31+
$shippingItemsInformation = [];
32+
foreach ($shippingAddress['cart_items'] as $cartItem) {
33+
$shippingItemsInformation[] = [
34+
$cartItem['cart_item_id'] => [
35+
'qty' => $cartItem['quantity'],
36+
'address' => $shippingAddress['customer_address_id']
37+
]
38+
];
39+
}
40+
41+
return $shippingItemsInformation;
42+
}
43+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SetShippingAddressOnCart/SingleShipping.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<?php
22
/**
3-
* @author Atwix Team
4-
* @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
77

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

10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1015
use Magento\Quote\Model\Quote\Address;
1116
use Magento\Quote\Model\ShippingAddressManagementInterface;
1217
use Magento\Customer\Api\AddressRepositoryInterface;
@@ -44,18 +49,46 @@ public function __construct(
4449
}
4550

4651
/**
52+
* @param ContextInterface $context
4753
* @param int $cartId
48-
* @param int|null $customerAddressId
49-
* @param array|null $address
54+
* @param array $shippingAddress
5055
* @return void
5156
*/
52-
public function setAddress(int $cartId, ?int $customerAddressId, ?array $address): void
57+
public function setAddress(ContextInterface $context, int $cartId, array $shippingAddress): void
5358
{
54-
if($customerAddressId) {
59+
$customerAddressId = $shippingAddress['customer_address_id'] ?? null;
60+
$addressInput = $shippingAddress['address'] ?? null;
61+
62+
if (!$customerAddressId && !$addressInput) {
63+
throw new GraphQlInputException(
64+
__('Shipping address should contain either "customer_address_id" or "address" input.')
65+
);
66+
}
67+
if ($customerAddressId && $addressInput) {
68+
throw new GraphQlInputException(
69+
__('Shipping address can\'t contain "customer_address_id" and "address" input at the same time.')
70+
);
71+
}
72+
if ($customerAddressId) {
73+
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
74+
throw new GraphQlAuthorizationException(
75+
__(
76+
'Address management allowed only for authorized customers'
77+
)
78+
);
79+
}
80+
/** @var AddressInterface $customerAddress */
5581
$customerAddress = $this->addressRepository->getById($customerAddressId);
82+
if ($context->getUserId() !== (int)$customerAddress->getCustomerId()) {
83+
throw new GraphQlInputException(
84+
__(
85+
'Address is not applicable for current customer'
86+
)
87+
);
88+
}
5689
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
5790
} else {
58-
$shippingAddress = $this->addressModel->addData($address);
91+
$shippingAddress = $this->addressModel->addData($addressInput);
5992
}
6093

6194
$this->shippingAddressManagement->assign($cartId, $shippingAddress);

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

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

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

10-
use Magento\Framework\Api\DataObjectHelper;
1110
use Magento\Framework\GraphQl\Config\Element\Field;
1211
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1312
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -22,11 +21,6 @@
2221
*/
2322
class SetShippingAddressesOnCart implements ResolverInterface
2423
{
25-
/**
26-
* @var DataObjectHelper
27-
*/
28-
private $dataObjectHelper;
29-
3024
/**
3125
* @var MaskedQuoteIdToQuoteIdInterface
3226
*/
@@ -48,20 +42,17 @@ class SetShippingAddressesOnCart implements ResolverInterface
4842
private $shippingAddressManagement;
4943

5044
/**
51-
* @param DataObjectHelper $dataObjectHelper
5245
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
5346
* @param MultiShipping $multiShipping
5447
* @param SingleShipping $singleShipping
5548
* @param ShippingAddressManagementInterface $shippingAddressManagement
5649
*/
5750
public function __construct(
58-
DataObjectHelper $dataObjectHelper,
5951
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
6052
MultiShipping $multiShipping,
6153
SingleShipping $singleShipping,
62-
ShippingAddressManagementInterface $shippingAddressManagement
54+
ShippingAddressManagementInterface $shippingAddressManagement
6355
) {
64-
$this->dataObjectHelper = $dataObjectHelper;
6556
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
6657
$this->multiShipping = $multiShipping;
6758
$this->singleShipping = $singleShipping;
@@ -76,23 +67,18 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7667
if (!isset($args['input']['cart_id'])) {
7768
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
7869
}
70+
if (!isset($args['input']['shipping_addresses'])) {
71+
throw new GraphQlInputException(__('Required parameter "shipping_addresses" is missing'));
72+
}
73+
74+
$shippingAddressesInput = $args['input']['shipping_addresses'];
7975
$maskedCartId = $args['input']['cart_id'];
8076
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
8177

82-
$customerAddressId = $args['input']['customer_address_id'] ?? null;
83-
$address = $args['input']['address'] ?? null;
84-
$cartItems = $args['input']['cart_items'] ?? [];
85-
86-
if (!$customerAddressId && !$address) {
87-
throw new GraphQlInputException(__('Query should contain either address id or address input.'));
88-
}
89-
90-
//TODO: how to determine whether is multi shipping or not
91-
if (!$cartItems) {
92-
//TODO: assign cart items
93-
$this->singleShipping->setAddress($cartId, $customerAddressId, $address);
78+
if (count($shippingAddressesInput) === 1) {
79+
$this->singleShipping->setAddress($context, $cartId, current($shippingAddressesInput));
9480
} else {
95-
$this->multiShipping->setAddress($cartId, $cartItems, $customerAddressId, $address);
81+
$this->multiShipping->setAddresses($context, $cartId, $shippingAddressesInput);
9682
}
9783

9884
//TODO: implement Cart object in the separate resolver

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ type Mutation {
1616

1717
input SetShippingAddressesOnCartInput {
1818
cart_id: String!
19+
shipping_addresses: [ShippingAddressInput!]!
20+
}
21+
22+
input ShippingAddressInput {
1923
customer_address_id: Int # Can be provided in one-page checkout and is required for multi-shipping checkout
2024
address: CartAddressInput
2125
cart_items: [CartItemQuantityInput!]

0 commit comments

Comments
 (0)