12
12
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
13
13
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
14
14
use Magento \Quote \Model \Quote \Address ;
15
+ use Magento \QuoteGraphQl \Model \Cart \Address \SaveQuoteAddressToCustomerAddressBook ;
15
16
16
17
/**
17
18
* Get shipping address
@@ -23,12 +24,21 @@ class GetShippingAddress
23
24
*/
24
25
private $ quoteAddressFactory ;
25
26
27
+ /**
28
+ * @var SaveQuoteAddressToCustomerAddressBook
29
+ */
30
+ private $ saveQuoteAddressToCustomerAddressBook ;
31
+
26
32
/**
27
33
* @param QuoteAddressFactory $quoteAddressFactory
34
+ * @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
28
35
*/
29
- public function __construct (QuoteAddressFactory $ quoteAddressFactory )
30
- {
36
+ public function __construct (
37
+ QuoteAddressFactory $ quoteAddressFactory ,
38
+ SaveQuoteAddressToCustomerAddressBook $ saveQuoteAddressToCustomerAddressBook
39
+ ) {
31
40
$ this ->quoteAddressFactory = $ quoteAddressFactory ;
41
+ $ this ->saveQuoteAddressToCustomerAddressBook = $ saveQuoteAddressToCustomerAddressBook ;
32
42
}
33
43
34
44
/**
@@ -62,16 +72,23 @@ public function execute(ContextInterface $context, array $shippingAddressInput):
62
72
);
63
73
}
64
74
75
+ $ customerId = $ context ->getUserId ();
76
+
65
77
if (null === $ customerAddressId ) {
66
78
$ shippingAddress = $ this ->quoteAddressFactory ->createBasedOnInputData ($ addressInput );
79
+
80
+ // need to save address only for registered user and if save_in_address_book = true
81
+ if (0 !== $ customerId && !empty ($ addressInput ['save_in_address_book ' ])) {
82
+ $ this ->saveQuoteAddressToCustomerAddressBook ->execute ($ shippingAddress , $ customerId );
83
+ }
67
84
} else {
68
85
if (false === $ context ->getExtensionAttributes ()->getIsCustomer ()) {
69
86
throw new GraphQlAuthorizationException (__ ('The current customer isn \'t authorized. ' ));
70
87
}
71
88
72
89
$ shippingAddress = $ this ->quoteAddressFactory ->createBasedOnCustomerAddress (
73
90
(int )$ customerAddressId ,
74
- $ context -> getUserId ()
91
+ $ customerId
75
92
);
76
93
}
77
94
0 commit comments