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

Commit 055bd1a

Browse files
authored
ENGCOM-6056: graphQl-509: save_in_address_book has no impact on Address Book #873
2 parents 0714971 + d3b4f62 commit 055bd1a

12 files changed

+477
-37
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
13+
use Magento\Customer\Api\Data\RegionInterface;
14+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
17+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
18+
19+
/**
20+
* Save Address to Customer Address Book.
21+
*/
22+
class SaveQuoteAddressToCustomerAddressBook
23+
{
24+
/**
25+
* @var AddressInterfaceFactory
26+
*/
27+
private $addressFactory;
28+
29+
/**
30+
* @var AddressRepositoryInterface
31+
*/
32+
private $addressRepository;
33+
34+
/**
35+
* @var RegionInterfaceFactory
36+
*/
37+
private $regionFactory;
38+
39+
/**
40+
* @param AddressInterfaceFactory $addressFactory
41+
* @param AddressRepositoryInterface $addressRepository
42+
* @param RegionInterfaceFactory $regionFactory
43+
*/
44+
public function __construct(
45+
AddressInterfaceFactory $addressFactory,
46+
AddressRepositoryInterface $addressRepository,
47+
RegionInterfaceFactory $regionFactory
48+
) {
49+
$this->addressFactory = $addressFactory;
50+
$this->addressRepository = $addressRepository;
51+
$this->regionFactory = $regionFactory;
52+
}
53+
54+
/**
55+
* Save Address to Customer Address Book.
56+
*
57+
* @param QuoteAddress $quoteAddress
58+
* @param int $customerId
59+
*
60+
* @return void
61+
* @throws GraphQlInputException
62+
*/
63+
public function execute(QuoteAddress $quoteAddress, int $customerId): void
64+
{
65+
try {
66+
/** @var AddressInterface $customerAddress */
67+
$customerAddress = $this->addressFactory->create();
68+
$customerAddress->setFirstname($quoteAddress->getFirstname())
69+
->setLastname($quoteAddress->getLastname())
70+
->setMiddlename($quoteAddress->getMiddlename())
71+
->setPrefix($quoteAddress->getPrefix())
72+
->setSuffix($quoteAddress->getSuffix())
73+
->setVatId($quoteAddress->getVatId())
74+
->setCountryId($quoteAddress->getCountryId())
75+
->setCompany($quoteAddress->getCompany())
76+
->setRegionId($quoteAddress->getRegionId())
77+
->setFax($quoteAddress->getFax())
78+
->setCity($quoteAddress->getCity())
79+
->setPostcode($quoteAddress->getPostcode())
80+
->setStreet($quoteAddress->getStreet())
81+
->setTelephone($quoteAddress->getTelephone())
82+
->setCustomerId($customerId);
83+
84+
/** @var RegionInterface $region */
85+
$region = $this->regionFactory->create();
86+
$region->setRegionCode($quoteAddress->getRegionCode())
87+
->setRegion($quoteAddress->getRegion())
88+
->setRegionId($quoteAddress->getRegionId());
89+
$customerAddress->setRegion($region);
90+
91+
$this->addressRepository->save($customerAddress);
92+
} catch (LocalizedException $e) {
93+
throw new GraphQlInputException(__($e->getMessage()), $e);
94+
}
95+
}
96+
}

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1414
use Magento\Quote\Model\Quote\Address;
15+
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;
1516

1617
/**
1718
* Get shipping address
@@ -23,12 +24,21 @@ class GetShippingAddress
2324
*/
2425
private $quoteAddressFactory;
2526

27+
/**
28+
* @var SaveQuoteAddressToCustomerAddressBook
29+
*/
30+
private $saveQuoteAddressToCustomerAddressBook;
31+
2632
/**
2733
* @param QuoteAddressFactory $quoteAddressFactory
34+
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
2835
*/
29-
public function __construct(QuoteAddressFactory $quoteAddressFactory)
30-
{
36+
public function __construct(
37+
QuoteAddressFactory $quoteAddressFactory,
38+
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
39+
) {
3140
$this->quoteAddressFactory = $quoteAddressFactory;
41+
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
3242
}
3343

3444
/**
@@ -62,16 +72,46 @@ public function execute(ContextInterface $context, array $shippingAddressInput):
6272
);
6373
}
6474

75+
$shippingAddress = $this->createShippingAddress($context, $customerAddressId, $addressInput);
76+
77+
return $shippingAddress;
78+
}
79+
80+
/**
81+
* Create shipping address.
82+
*
83+
* @param ContextInterface $context
84+
* @param int|null $customerAddressId
85+
* @param array|null $addressInput
86+
*
87+
* @return \Magento\Quote\Model\Quote\Address
88+
* @throws GraphQlAuthorizationException
89+
*/
90+
private function createShippingAddress(
91+
ContextInterface $context,
92+
?int $customerAddressId,
93+
?array $addressInput
94+
) {
95+
$customerId = $context->getUserId();
96+
6597
if (null === $customerAddressId) {
6698
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
99+
100+
// need to save address only for registered user and if save_in_address_book = true
101+
if (0 !== $customerId
102+
&& isset($addressInput['save_in_address_book'])
103+
&& (bool)$addressInput['save_in_address_book'] === true
104+
) {
105+
$this->saveQuoteAddressToCustomerAddressBook->execute($shippingAddress, $customerId);
106+
}
67107
} else {
68108
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
69109
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
70110
}
71111

72112
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
73113
(int)$customerAddressId,
74-
$context->getUserId()
114+
$customerId
75115
);
76116
}
77117

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\GraphQl\Model\Query\ContextInterface;
1414
use Magento\Quote\Api\Data\CartInterface;
1515
use Magento\Quote\Model\Quote\Address;
16+
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;
1617

1718
/**
1819
* Set billing address for a specified shopping cart
@@ -29,16 +30,24 @@ class SetBillingAddressOnCart
2930
*/
3031
private $assignBillingAddressToCart;
3132

33+
/**
34+
* @var SaveQuoteAddressToCustomerAddressBook
35+
*/
36+
private $saveQuoteAddressToCustomerAddressBook;
37+
3238
/**
3339
* @param QuoteAddressFactory $quoteAddressFactory
3440
* @param AssignBillingAddressToCart $assignBillingAddressToCart
41+
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
3542
*/
3643
public function __construct(
3744
QuoteAddressFactory $quoteAddressFactory,
38-
AssignBillingAddressToCart $assignBillingAddressToCart
45+
AssignBillingAddressToCart $assignBillingAddressToCart,
46+
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
3947
) {
4048
$this->quoteAddressFactory = $quoteAddressFactory;
4149
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
50+
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
4251
}
4352

4453
/**
@@ -101,6 +110,15 @@ private function createBillingAddress(
101110
): Address {
102111
if (null === $customerAddressId) {
103112
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
113+
114+
$customerId = $context->getUserId();
115+
// need to save address only for registered user and if save_in_address_book = true
116+
if (0 !== $customerId
117+
&& isset($addressInput['save_in_address_book'])
118+
&& (bool)$addressInput['save_in_address_book'] === true
119+
) {
120+
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
121+
}
104122
} else {
105123
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
106124
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
@@ -111,6 +129,7 @@ private function createBillingAddress(
111129
(int)$context->getUserId()
112130
);
113131
}
132+
114133
return $billingAddress;
115134
}
116135
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1110
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1211
use Magento\GraphQl\Model\Query\ContextInterface;
1312
use Magento\Quote\Api\Data\CartInterface;
@@ -21,6 +20,7 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
2120
* @var AssignShippingAddressToCart
2221
*/
2322
private $assignShippingAddressToCart;
23+
2424
/**
2525
* @var GetShippingAddress
2626
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ input CartAddressInput {
109109
postcode: String
110110
country_code: String!
111111
telephone: String!
112-
save_in_address_book: Boolean!
112+
save_in_address_book: Boolean
113113
}
114114

115115
input SetShippingMethodsOnCartInput {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ private function setBillingAddress(string $cartId): void
259259
telephone: "88776655"
260260
region: "TX"
261261
country_code: "US"
262-
save_in_address_book: false
263262
}
264263
}
265264
}
@@ -298,7 +297,6 @@ private function setShippingAddress(string $cartId): array
298297
postcode: "887766"
299298
country_code: "US"
300299
telephone: "88776655"
301-
save_in_address_book: false
302300
}
303301
}
304302
]

0 commit comments

Comments
 (0)