Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 93498ff

Browse files
authored
ENGCOM-4219: GraphQL 296 set billing address #321
2 parents 2747b23 + dc261fe commit 93498ff

12 files changed

+953
-23
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\Cart\Address;
9+
10+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\QuoteGraphQl\Model\Cart\Address\Mapper\Address;
15+
16+
/**
17+
* Collect and return information about a billing address
18+
*/
19+
class BillingAddressDataProvider
20+
{
21+
/**
22+
* @var CartRepositoryInterface
23+
*/
24+
private $cartRepository;
25+
26+
/**
27+
* @var Address
28+
*/
29+
private $addressMapper;
30+
31+
/**
32+
* @var ExtensibleDataObjectConverter
33+
*/
34+
private $dataObjectConverter;
35+
36+
/**
37+
* AddressDataProvider constructor.
38+
*
39+
* @param CartRepositoryInterface $cartRepository
40+
* @param Address $addressMapper
41+
* @param ExtensibleDataObjectConverter $dataObjectConverter
42+
*/
43+
public function __construct(
44+
CartRepositoryInterface $cartRepository,
45+
Address $addressMapper,
46+
ExtensibleDataObjectConverter $dataObjectConverter
47+
) {
48+
$this->cartRepository = $cartRepository;
49+
$this->addressMapper = $addressMapper;
50+
$this->dataObjectConverter = $dataObjectConverter;
51+
}
52+
53+
/**
54+
* Collect and return information about a billing addresses
55+
*
56+
* @param CartInterface $cart
57+
* @return null|array
58+
*/
59+
public function getCartAddresses(CartInterface $cart): ?array
60+
{
61+
$cart = $this->cartRepository->get($cart->getId());
62+
$billingAddress = $cart->getBillingAddress();
63+
64+
if (!$billingAddress) {
65+
return null;
66+
}
67+
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
68+
$addressData = array_merge($billingData, $this->addressMapper->toNestedArray($billingAddress));
69+
70+
return $addressData;
71+
}
72+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Cart\Address\Mapper;
9+
10+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
11+
12+
/**
13+
* Class Address
14+
*
15+
* Extract the necessary address fields from an Address model
16+
*/
17+
class Address
18+
{
19+
20+
/**
21+
* Converts Address model data to nested array
22+
*
23+
* @param QuoteAddress $address
24+
* @return array
25+
*/
26+
public function toNestedArray(QuoteAddress $address): array
27+
{
28+
$addressData = [
29+
'country' => [
30+
'code' => $address->getCountryId(),
31+
'label' => $address->getCountry()
32+
],
33+
'region' => [
34+
'code' => $address->getRegionCode(),
35+
'label' => $address->getRegion()
36+
],
37+
'street' => $address->getStreet(),
38+
'selected_shipping_method' => [
39+
'code' => $address->getShippingMethod(),
40+
'label' => $address->getShippingDescription(),
41+
'free_shipping' => $address->getFreeShipping(),
42+
],
43+
'items_weight' => $address->getWeight(),
44+
'customer_notes' => $address->getCustomerNotes()
45+
];
46+
47+
if (!$address->hasItems()) {
48+
return $addressData;
49+
}
50+
51+
$addressItemsData = [];
52+
foreach ($address->getAllItems() as $addressItem) {
53+
$addressItemsData[] = [
54+
'cart_item_id' => $addressItem->getQuoteItemId(),
55+
'quantity' => $addressItem->getQty()
56+
];
57+
}
58+
$addressData['cart_items'] = $addressItemsData;
59+
60+
return $addressData;
61+
}
62+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Cart\Address;
9+
10+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\QuoteGraphQl\Model\Cart\Address\Mapper\Address;
15+
16+
/**
17+
* Class AddressDataProvider
18+
*
19+
* Collect and return information about cart shipping and billing addresses
20+
*/
21+
class ShippingAddressesDataProvider
22+
{
23+
/**
24+
* @var ExtensibleDataObjectConverter
25+
*/
26+
private $dataObjectConverter;
27+
28+
/**
29+
* @var CartRepositoryInterface
30+
*/
31+
private $cartRepository;
32+
33+
/**
34+
* @var Address
35+
*/
36+
private $addressMapper;
37+
38+
/**
39+
* AddressDataProvider constructor.
40+
*
41+
* @param ExtensibleDataObjectConverter $dataObjectConverter
42+
* @param CartRepositoryInterface $cartRepository
43+
* @param Address $addressMapper
44+
*/
45+
public function __construct(
46+
ExtensibleDataObjectConverter $dataObjectConverter,
47+
CartRepositoryInterface $cartRepository,
48+
Address $addressMapper
49+
) {
50+
$this->dataObjectConverter = $dataObjectConverter;
51+
$this->cartRepository = $cartRepository;
52+
$this->addressMapper = $addressMapper;
53+
}
54+
55+
/**
56+
* Collect and return information about shipping addresses
57+
*
58+
* @param CartInterface $cart
59+
* @return array
60+
*/
61+
public function getCartAddresses(CartInterface $cart): array
62+
{
63+
$cart = $this->cartRepository->get($cart->getId());
64+
$addressData = [];
65+
$shippingAddresses = $cart->getAllShippingAddresses();
66+
67+
if ($shippingAddresses) {
68+
foreach ($shippingAddresses as $shippingAddress) {
69+
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
70+
$addressData[] = array_merge($shippingData, $this->addressMapper->toNestedArray($shippingAddress));
71+
}
72+
}
73+
74+
return $addressData;
75+
}
76+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\Cart;
9+
10+
use Magento\Customer\Api\Data\AddressInterface;
11+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Quote\Api\Data\CartInterface;
15+
use Magento\Quote\Model\Quote\Address;
16+
use Magento\Quote\Api\BillingAddressManagementInterface;
17+
use Magento\Customer\Api\AddressRepositoryInterface;
18+
19+
/**
20+
* Set billing address for a specified shopping cart
21+
*/
22+
class SetBillingAddressOnCart
23+
{
24+
/**
25+
* @var BillingAddressManagementInterface
26+
*/
27+
private $billingAddressManagement;
28+
29+
/**
30+
* @var AddressRepositoryInterface
31+
*/
32+
private $addressRepository;
33+
34+
/**
35+
* @var Address
36+
*/
37+
private $addressModel;
38+
39+
/**
40+
* @var CheckCustomerAccount
41+
*/
42+
private $checkCustomerAccount;
43+
44+
/**
45+
* @param BillingAddressManagementInterface $billingAddressManagement
46+
* @param AddressRepositoryInterface $addressRepository
47+
* @param Address $addressModel
48+
* @param CheckCustomerAccount $checkCustomerAccount
49+
*/
50+
public function __construct(
51+
BillingAddressManagementInterface $billingAddressManagement,
52+
AddressRepositoryInterface $addressRepository,
53+
Address $addressModel,
54+
CheckCustomerAccount $checkCustomerAccount
55+
) {
56+
$this->billingAddressManagement = $billingAddressManagement;
57+
$this->addressRepository = $addressRepository;
58+
$this->addressModel = $addressModel;
59+
$this->checkCustomerAccount = $checkCustomerAccount;
60+
}
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
66+
{
67+
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
68+
$addressInput = $billingAddress['address'] ?? null;
69+
$useForShipping = $billingAddress['use_for_shipping'] ?? false;
70+
71+
if (null === $customerAddressId && null === $addressInput) {
72+
throw new GraphQlInputException(
73+
__('The billing address must contain either "customer_address_id" or "address".')
74+
);
75+
}
76+
if ($customerAddressId && $addressInput) {
77+
throw new GraphQlInputException(
78+
__('The billing address cannot contain "customer_address_id" and "address" at the same time.')
79+
);
80+
}
81+
$addresses = $cart->getAllShippingAddresses();
82+
if ($useForShipping && count($addresses) > 1) {
83+
throw new GraphQlInputException(
84+
__('Using the "use_for_shipping" option with multishipping is not possible.')
85+
);
86+
}
87+
if (null === $customerAddressId) {
88+
$billingAddress = $this->addressModel->addData($addressInput);
89+
} else {
90+
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
91+
92+
/** @var AddressInterface $customerAddress */
93+
$customerAddress = $this->addressRepository->getById($customerAddressId);
94+
$billingAddress = $this->addressModel->importCustomerAddressData($customerAddress);
95+
}
96+
97+
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
98+
}
99+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\QuoteGraphQl\Model\Cart\Address\BillingAddressDataProvider;
15+
16+
/**
17+
* @inheritdoc
18+
*/
19+
class BillingAddress implements ResolverInterface
20+
{
21+
/**
22+
* @var BillingAddressDataProvider
23+
*/
24+
private $addressDataProvider;
25+
26+
/**
27+
* @param BillingAddressDataProvider $addressDataProvider
28+
*/
29+
public function __construct(
30+
BillingAddressDataProvider $addressDataProvider
31+
) {
32+
$this->addressDataProvider = $addressDataProvider;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
39+
{
40+
if (!isset($value['model'])) {
41+
throw new LocalizedException(__('"model" value should be specified'));
42+
}
43+
44+
$cart = $value['model'];
45+
46+
return $this->addressDataProvider->getCartAddresses($cart);
47+
}
48+
}

0 commit comments

Comments
 (0)