Skip to content

Commit f29efd3

Browse files
committed
graphQl-509: save_in_address_book has no impact on Address Book
1 parent ad44545 commit f29efd3

File tree

4 files changed

+131
-4
lines changed

4 files changed

+131
-4
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
*
56+
* @param QuoteAddress $quoteAddress
57+
* @param int $customerId
58+
*
59+
* @return void
60+
* @throws GraphQlInputException
61+
*/
62+
public function execute(QuoteAddress $quoteAddress, int $customerId): void
63+
{
64+
try {
65+
/** @var AddressInterface $customerAddress */
66+
$customerAddress = $this->addressFactory->create();
67+
$customerAddress->setFirstname($quoteAddress->getFirstname())
68+
->setLastname($quoteAddress->getLastname())
69+
->setMiddlename($quoteAddress->getMiddlename())
70+
->setPrefix($quoteAddress->getPrefix())
71+
->setSuffix($quoteAddress->getSuffix())
72+
->setVatId($quoteAddress->getVatId())
73+
->setCountryId($quoteAddress->getCountryId())
74+
->setCompany($quoteAddress->getCompany())
75+
->setRegionId($quoteAddress->getRegionId())
76+
->setFax($quoteAddress->getFax())
77+
->setCity($quoteAddress->getCity())
78+
->setPostcode($quoteAddress->getPostcode())
79+
->setStreet($quoteAddress->getStreet())
80+
->setTelephone($quoteAddress->getTelephone())
81+
->setCustomerId($customerId);
82+
83+
/** @var RegionInterface $region */
84+
$region = $this->regionFactory->create();
85+
$region->setRegionCode($quoteAddress->getRegionCode())
86+
->setRegion($quoteAddress->getRegion())
87+
->setRegionId($quoteAddress->getRegionId());
88+
$customerAddress->setRegion($region);
89+
90+
$this->addressRepository->save($customerAddress);
91+
} catch (LocalizedException $e) {
92+
throw new GraphQlInputException(__($e->getMessage()), $e);
93+
}
94+
}
95+
}

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

Lines changed: 17 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,12 @@ 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 && !empty($addressInput['save_in_address_book'])) {
117+
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
118+
}
104119
} else {
105120
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
106121
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
@@ -111,6 +126,7 @@ private function createBillingAddress(
111126
(int)$context->getUserId()
112127
);
113128
}
129+
114130
return $billingAddress;
115131
}
116132
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1212
use Magento\GraphQl\Model\Query\ContextInterface;
1313
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;
1415

1516
/**
1617
* Set single shipping address for a specified shopping cart
@@ -27,16 +28,24 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
2728
*/
2829
private $assignShippingAddressToCart;
2930

31+
/**
32+
* @var SaveQuoteAddressToCustomerAddressBook
33+
*/
34+
private $saveQuoteAddressToCustomerAddressBook;
35+
3036
/**
3137
* @param QuoteAddressFactory $quoteAddressFactory
3238
* @param AssignShippingAddressToCart $assignShippingAddressToCart
39+
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
3340
*/
3441
public function __construct(
3542
QuoteAddressFactory $quoteAddressFactory,
36-
AssignShippingAddressToCart $assignShippingAddressToCart
43+
AssignShippingAddressToCart $assignShippingAddressToCart,
44+
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
3745
) {
3846
$this->quoteAddressFactory = $quoteAddressFactory;
3947
$this->assignShippingAddressToCart = $assignShippingAddressToCart;
48+
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
4049
}
4150

4251
/**
@@ -65,16 +74,23 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
6574
);
6675
}
6776

77+
$customerId = $context->getUserId();
78+
6879
if (null === $customerAddressId) {
6980
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
81+
82+
// need to save address only for registered user and if save_in_address_book = true
83+
if (0 !== $customerId && !empty($addressInput['save_in_address_book'])) {
84+
$this->saveQuoteAddressToCustomerAddressBook->execute($shippingAddress, $customerId);
85+
}
7086
} else {
7187
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
7288
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
7389
}
7490

7591
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
7692
(int)$customerAddressId,
77-
$context->getUserId()
93+
$customerId
7894
);
7995
}
8096

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ input CartAddressInput {
108108
postcode: String
109109
country_code: String!
110110
telephone: String!
111-
save_in_address_book: Boolean!
111+
save_in_address_book: Boolean
112112
}
113113

114114
input SetShippingMethodsOnCartInput {

0 commit comments

Comments
 (0)