Skip to content

Commit ef8f126

Browse files
author
vitaliyboyko
committed
graph-ql: concept of shipping address coverage, added separate flows for multi shipping and single shipping
1 parent e327bb6 commit ef8f126

File tree

3 files changed

+162
-33
lines changed

3 files changed

+162
-33
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @author Atwix Team
4+
* @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress\SetShippingAddressOnCart;
9+
10+
class MultiShipping
11+
{
12+
/**
13+
* @param int $cartId
14+
* @param array $cartItems
15+
* @param int|null $customerAddressId
16+
* @param array|null $address
17+
* @return void
18+
*/
19+
public function setAddress(int $cartId, array $cartItems, ?int $customerAddressId, ?array $address): void
20+
{
21+
//TODO: implement multi shipping
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* @author Atwix Team
4+
* @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress\SetShippingAddressOnCart;
9+
10+
use Magento\Quote\Model\Quote\Address;
11+
use Magento\Quote\Model\ShippingAddressManagementInterface;
12+
use Magento\Customer\Api\AddressRepositoryInterface;
13+
14+
class SingleShipping
15+
{
16+
/**
17+
* @var ShippingAddressManagementInterface
18+
*/
19+
private $shippingAddressManagement;
20+
21+
/**
22+
* @var AddressRepositoryInterface
23+
*/
24+
private $addressRepository;
25+
26+
/**
27+
* @var Address
28+
*/
29+
private $addressModel;
30+
31+
/**
32+
* @param ShippingAddressManagementInterface $shippingAddressManagement
33+
* @param AddressRepositoryInterface $addressRepository
34+
* @param Address $addressModel
35+
*/
36+
public function __construct(
37+
ShippingAddressManagementInterface $shippingAddressManagement,
38+
AddressRepositoryInterface $addressRepository,
39+
Address $addressModel
40+
) {
41+
$this->shippingAddressManagement = $shippingAddressManagement;
42+
$this->addressRepository = $addressRepository;
43+
$this->addressModel = $addressModel;
44+
}
45+
46+
/**
47+
* @param int $cartId
48+
* @param int|null $customerAddressId
49+
* @param array|null $address
50+
* @return void
51+
*/
52+
public function setAddress(int $cartId, ?int $customerAddressId, ?array $address): void
53+
{
54+
if($customerAddressId) {
55+
$customerAddress = $this->addressRepository->getById($customerAddressId);
56+
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
57+
} else {
58+
$shippingAddress = $this->addressModel->addData($address);
59+
}
60+
61+
$this->shippingAddressManagement->assign($cartId, $shippingAddress);
62+
}
63+
}

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

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,65 @@
77

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

10-
use Magento\Customer\Api\AddressRepositoryInterface;
1110
use Magento\Framework\Api\DataObjectHelper;
1211
use Magento\Framework\GraphQl\Config\Element\Field;
1312
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
1514
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1615
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
17-
use Magento\Quote\Model\Quote\Address;
1816
use Magento\Quote\Model\ShippingAddressManagementInterface;
17+
use Magento\QuoteGraphQl\Model\Resolver\ShippingAddress\SetShippingAddressOnCart\MultiShipping;
18+
use Magento\QuoteGraphQl\Model\Resolver\ShippingAddress\SetShippingAddressOnCart\SingleShipping;
1919

2020
/**
2121
* @inheritdoc
2222
*/
2323
class SetShippingAddressesOnCart implements ResolverInterface
2424
{
2525
/**
26-
* @var ShippingAddressManagementInterface
26+
* @var DataObjectHelper
2727
*/
28-
private $shippingAddressManagement;
28+
private $dataObjectHelper;
2929

3030
/**
31-
* @var AddressRepositoryInterface
31+
* @var MaskedQuoteIdToQuoteIdInterface
3232
*/
33-
private $addressRepository;
33+
private $maskedQuoteIdToQuoteId;
3434

3535
/**
36-
* @var Address
36+
* @var MultiShipping
3737
*/
38-
private $addressModel;
38+
private $multiShipping;
39+
3940
/**
40-
* @var DataObjectHelper
41+
* @var SingleShipping
4142
*/
42-
private $dataObjectHelper;
43+
private $singleShipping;
4344

4445
/**
45-
* @var MaskedQuoteIdToQuoteIdInterface
46+
* @var ShippingAddressManagementInterface
4647
*/
47-
private $maskedQuoteIdToQuoteId;
48+
private $shippingAddressManagement;
4849

4950
/**
50-
* @param ShippingAddressManagementInterface $shippingAddressManagement
51-
* @param AddressRepositoryInterface $addressRepository
52-
* @param Address $addressModel
5351
* @param DataObjectHelper $dataObjectHelper
5452
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
53+
* @param MultiShipping $multiShipping
54+
* @param SingleShipping $singleShipping
55+
* @param ShippingAddressManagementInterface $shippingAddressManagement
5556
*/
5657
public function __construct(
57-
ShippingAddressManagementInterface $shippingAddressManagement,
58-
AddressRepositoryInterface $addressRepository,
59-
Address $addressModel,
6058
DataObjectHelper $dataObjectHelper,
61-
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
59+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
60+
MultiShipping $multiShipping,
61+
SingleShipping $singleShipping,
62+
ShippingAddressManagementInterface $shippingAddressManagement
6263
) {
63-
$this->shippingAddressManagement = $shippingAddressManagement;
64-
$this->addressRepository = $addressRepository;
65-
$this->addressModel = $addressModel;
6664
$this->dataObjectHelper = $dataObjectHelper;
6765
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
66+
$this->multiShipping = $multiShipping;
67+
$this->singleShipping = $singleShipping;
68+
$this->shippingAddressManagement = $shippingAddressManagement;
6869
}
6970

7071
/**
@@ -78,27 +79,69 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7879
$maskedCartId = $args['input']['cart_id'];
7980
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
8081

81-
$customerAddressId = $args['input']['customer_address_id'] ?? 0;
82+
$customerAddressId = $args['input']['customer_address_id'] ?? null;
8283
$address = $args['input']['address'] ?? null;
8384
$cartItems = $args['input']['cart_items'] ?? [];
8485

8586
if (!$customerAddressId && !$address) {
8687
throw new GraphQlInputException(__('Query should contain either address id or address input.'));
8788
}
8889

90+
//TODO: how to determine whether is multi shipping or not
8991
if (!$cartItems) {
90-
if($customerAddressId) {
91-
$customerAddress = $this->addressRepository->getById($customerAddressId);
92-
$shippingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
93-
$this->shippingAddressManagement->assign($cartId, $shippingAddress);
94-
} else {
95-
$shippingAddress = $this->addressModel->addData($address);
96-
$this->shippingAddressManagement->assign($cartId, $shippingAddress);
97-
}
92+
//TODO: assign cart items
93+
$this->singleShipping->setAddress($cartId, $customerAddressId, $address);
9894
} else {
99-
//TODO: implement multi shipping address assign flow
95+
$this->multiShipping->setAddress($cartId, $cartItems, $customerAddressId, $address);
10096
}
10197

102-
return [];
98+
//TODO: implement Cart object in the separate resolver
99+
$shippingAddress = $this->shippingAddressManagement->get($cartId);
100+
return [
101+
'cart' => [
102+
'applied_coupon' => [
103+
'code' => ''
104+
],
105+
'addresses' => [[
106+
'firstname' => $shippingAddress->getFirstname(),
107+
'lastname' => $shippingAddress->getLastname(),
108+
'company' => $shippingAddress->getCompany(),
109+
'street' => $shippingAddress->getStreet(),
110+
'city' => $shippingAddress->getCity(),
111+
'region' => [
112+
'code' => $shippingAddress->getRegionCode(),
113+
'label' => $shippingAddress->getRegion()
114+
],
115+
'country' => [
116+
'code' => $shippingAddress->getCountryId(),
117+
'label' => ''
118+
],
119+
'postcode' => $shippingAddress->getPostcode(),
120+
'telephone' => $shippingAddress->getTelephone(),
121+
'address_type' => 'SHIPPING',
122+
'selected_shipping_method' => [
123+
'code' => 'test',
124+
'label' => 'test',
125+
'free_shipping' => 'test',
126+
'error_message' => 'test'
127+
],
128+
'available_shipping_methods' => [[
129+
'code' => 'test',
130+
'label' => 'test',
131+
'free_shipping' => 'test',
132+
'error_message' => 'test'
133+
]],
134+
'items_weight' => [0],
135+
'customer_notes' => $shippingAddress->getLastname(),
136+
'cart_items' => [[
137+
'cart_item_id' => '',
138+
'quantity' => 0
139+
]],
140+
'applied_coupon' => [
141+
'code' => ''
142+
]
143+
]]
144+
]
145+
];
103146
}
104147
}

0 commit comments

Comments
 (0)