3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
- declare (strict_types= 1 );
6
+ declare (strict_types = 1 );
7
7
8
- namespace Magento \Checkout \Plugin \ Model ;
8
+ namespace Magento \Checkout \Model ;
9
9
10
- use Magento \Checkout \Api \PaymentInformationManagementInterface ;
11
10
use Magento \Customer \Api \AddressRepositoryInterface ;
12
11
use Magento \Framework \Exception \LocalizedException ;
13
12
use Magento \Quote \Api \CartRepositoryInterface ;
14
13
use Magento \Quote \Api \Data \AddressInterface ;
15
14
use Magento \Quote \Api \Data \PaymentInterface ;
16
15
use Magento \Quote \Model \Quote ;
16
+ use Magento \Quote \Model \QuoteIdMaskFactory ;
17
17
18
- /**
19
- * Class PaymentInformationManagement
20
- */
21
- class PaymentInformationManagementPlugin
18
+ class AddressMapper implements AddressMapperInterface
22
19
{
23
20
/**
24
21
* @var CartRepositoryInterface
25
22
*/
26
- private $ quoteRepository ;
23
+ private $ cartRepository ;
27
24
28
25
/**
29
26
* @var AddressRepositoryInterface
30
27
*/
31
28
private $ addressRepository ;
32
29
33
30
/**
34
- * PaymentInformationManagement constructor
31
+ * @var QuoteIdMaskFactory
32
+ */
33
+ private $ quoteIdMaskFactory ;
34
+
35
+ /**
36
+ * AddressMapper constructor
35
37
*
36
- * @param CartRepositoryInterface $quoteRepository
38
+ * @param CartRepositoryInterface $cartRepository
37
39
* @param AddressRepositoryInterface $addressRepository
40
+ * @param QuoteIdMaskFactory $quoteIdMaskFactory
38
41
*/
39
42
public function __construct (
40
- CartRepositoryInterface $ quoteRepository ,
41
- AddressRepositoryInterface $ addressRepository
43
+ CartRepositoryInterface $ cartRepository ,
44
+ AddressRepositoryInterface $ addressRepository ,
45
+ QuoteIdMaskFactory $ quoteIdMaskFactory
42
46
) {
43
- $ this ->quoteRepository = $ quoteRepository ;
47
+ $ this ->cartRepository = $ cartRepository ;
44
48
$ this ->addressRepository = $ addressRepository ;
49
+ $ this ->quoteIdMaskFactory = $ quoteIdMaskFactory ;
45
50
}
46
51
47
52
/**
48
- * Disable order submitting for preview
49
- *
50
- * @param PaymentInformationManagementInterface $subject
51
- * @param int $cartId
52
- * @param PaymentInterface $paymentMethod
53
- * @param AddressInterface|null $billingAddress
54
- * @return void
53
+ * @inheritDoc
55
54
* @throws LocalizedException
56
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
57
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
58
55
*/
59
- public function beforeSavePaymentInformationAndPlaceOrder (
60
- PaymentInformationManagementInterface $ subject ,
56
+ public function customerCheckoutAddressMapper (
61
57
int $ cartId ,
62
58
PaymentInterface $ paymentMethod ,
63
59
AddressInterface $ billingAddress = null
64
60
): void {
65
61
/** @var Quote $quote */
66
- $ quote = $ this ->quoteRepository ->getActive ($ cartId );
62
+ $ quote = $ this ->cartRepository ->getActive ($ cartId );
67
63
$ shippingAddress = $ quote ->getShippingAddress ();
68
64
$ quoteShippingAddressData = $ shippingAddress ->getData ();
69
- $ quoteSameAsBilling = (int ) $ shippingAddress ->getSameAsBilling ();
65
+ $ quoteSameAsBilling = (int )$ shippingAddress ->getSameAsBilling ();
70
66
$ customer = $ quote ->getCustomer ();
71
67
$ customerId = $ customer ->getId ();
72
68
$ hasDefaultBilling = $ customer ->getDefaultBilling ();
@@ -76,7 +72,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
76
72
$ sameAsBillingFlag = 1 ;
77
73
} elseif (!empty ($ quoteShippingAddressData ) && !empty ($ billingAddress )) {
78
74
$ sameAsBillingFlag = $ quote ->getCustomerId () &&
79
- $ this ->checkIfShippingNullOrNotSameAsBillingAddress ($ shippingAddress , $ billingAddress );
75
+ $ this ->checkIfShippingAddressMatchesWithBillingAddress ($ shippingAddress , $ billingAddress );
80
76
} else {
81
77
$ sameAsBillingFlag = 0 ;
82
78
}
@@ -97,13 +93,74 @@ public function beforeSavePaymentInformationAndPlaceOrder(
97
93
}
98
94
99
95
/**
100
- * Returns true if shipping address is same as billing or it is undefined
96
+ * @inheritDoc
97
+ */
98
+ public function guestCheckoutAddressMapper (
99
+ string $ cartId ,
100
+ string $ email ,
101
+ PaymentInterface $ paymentMethod ,
102
+ AddressInterface $ billingAddress = null
103
+ ): void {
104
+ $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ cartId , 'masked_id ' );
105
+ /** @var Quote $quote */
106
+ $ quote = $ this ->cartRepository ->getActive ($ quoteIdMask ->getQuoteId ());
107
+ $ shippingAddress = $ quote ->getShippingAddress ();
108
+
109
+ if (!empty ($ billingAddress )) {
110
+ $ sameAsBillingFlag = $ this ->checkIfShippingAddressMatchesWithBillingAddress ($ shippingAddress , $ billingAddress );
111
+ } else {
112
+ $ sameAsBillingFlag = 0 ;
113
+ }
114
+
115
+ if ($ sameAsBillingFlag ) {
116
+ $ shippingAddress ->setSameAsBilling (1 );
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Process customer shipping address
122
+ *
123
+ * @param Quote $quote
124
+ * @return void
125
+ * @throws LocalizedException
126
+ */
127
+ private function processCustomerShippingAddress (Quote $ quote ): void
128
+ {
129
+ $ shippingAddress = $ quote ->getShippingAddress ();
130
+ $ billingAddress = $ quote ->getBillingAddress ();
131
+
132
+ $ customer = $ quote ->getCustomer ();
133
+ $ hasDefaultBilling = $ customer ->getDefaultBilling ();
134
+ $ hasDefaultShipping = $ customer ->getDefaultShipping ();
135
+
136
+ if ($ shippingAddress ->getQuoteId ()) {
137
+ $ shippingAddressData = $ shippingAddress ->exportCustomerAddress ();
138
+ }
139
+ if (isset ($ shippingAddressData )) {
140
+ if (!$ hasDefaultShipping ) {
141
+ //Make provided address as default shipping address
142
+ $ shippingAddressData ->setIsDefaultShipping (true );
143
+ if (!$ hasDefaultBilling && !$ billingAddress ->getSaveInAddressBook ()) {
144
+ $ shippingAddressData ->setIsDefaultBilling (true );
145
+ }
146
+ }
147
+ //save here new customer address
148
+ $ shippingAddressData ->setCustomerId ($ quote ->getCustomerId ());
149
+ $ this ->addressRepository ->save ($ shippingAddressData );
150
+ $ quote ->addCustomerAddress ($ shippingAddressData );
151
+ $ shippingAddress ->setCustomerAddressData ($ shippingAddressData );
152
+ $ shippingAddress ->setCustomerAddressId ($ shippingAddressData ->getId ());
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Returns true if shipping address is same as billing, or it is undefined
101
158
*
102
159
* @param AddressInterface $shippingAddress
103
160
* @param AddressInterface $billingAddress
104
161
* @return bool
105
162
*/
106
- private function checkIfShippingNullOrNotSameAsBillingAddress (
163
+ private function checkIfShippingAddressMatchesWithBillingAddress (
107
164
AddressInterface $ shippingAddress ,
108
165
AddressInterface $ billingAddress
109
166
): bool {
@@ -141,7 +198,7 @@ private function convertAddressValueToFlatArray(array $address): array
141
198
{
142
199
array_walk (
143
200
$ address ,
144
- function (&$ value ) {
201
+ static function (&$ value ) {
145
202
if (is_array ($ value ) && isset ($ value ['value ' ])) {
146
203
if (!is_array ($ value ['value ' ])) {
147
204
$ value = (string )$ value ['value ' ];
@@ -153,40 +210,4 @@ function (&$value) {
153
210
);
154
211
return $ address ;
155
212
}
156
-
157
- /**
158
- * Process customer shipping address
159
- *
160
- * @param Quote $quote
161
- * @return void
162
- * @throws LocalizedException
163
- */
164
- private function processCustomerShippingAddress (Quote $ quote ): void
165
- {
166
- $ shippingAddress = $ quote ->getShippingAddress ();
167
- $ billingAddress = $ quote ->getBillingAddress ();
168
-
169
- $ customer = $ quote ->getCustomer ();
170
- $ hasDefaultBilling = $ customer ->getDefaultBilling ();
171
- $ hasDefaultShipping = $ customer ->getDefaultShipping ();
172
-
173
- if ($ shippingAddress ->getQuoteId ()) {
174
- $ shippingAddressData = $ shippingAddress ->exportCustomerAddress ();
175
- }
176
- if (isset ($ shippingAddressData )) {
177
- if (!$ hasDefaultShipping ) {
178
- //Make provided address as default shipping address
179
- $ shippingAddressData ->setIsDefaultShipping (true );
180
- if (!$ hasDefaultBilling && !$ billingAddress ->getSaveInAddressBook ()) {
181
- $ shippingAddressData ->setIsDefaultBilling (true );
182
- }
183
- }
184
- //save here new customer address
185
- $ shippingAddressData ->setCustomerId ($ quote ->getCustomerId ());
186
- $ this ->addressRepository ->save ($ shippingAddressData );
187
- $ quote ->addCustomerAddress ($ shippingAddressData );
188
- $ shippingAddress ->setCustomerAddressData ($ shippingAddressData );
189
- $ shippingAddress ->setCustomerAddressId ($ shippingAddressData ->getId ());
190
- }
191
- }
192
213
}
0 commit comments