3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Quote \Model ;
8
9
9
- use Magento \Customer \Api \CustomerRepositoryInterface as CustomerRepository ;
10
10
use Magento \Customer \Api \AccountManagementInterface as AccountManagement ;
11
11
use Magento \Customer \Api \AddressRepositoryInterface as CustomerAddressRepository ;
12
- use Magento \Quote \Model \Quote as QuoteEntity ;
12
+ use Magento \Customer \Api \CustomerRepositoryInterface as CustomerRepository ;
13
+ use Magento \Customer \Model \AddressFactory ;
13
14
use Magento \Framework \App \ObjectManager ;
15
+ use Magento \Framework \Validator \Exception as ValidatorException ;
16
+ use Magento \Framework \Validator \Factory as ValidatorFactory ;
17
+ use Magento \Quote \Model \Quote as QuoteEntity ;
14
18
15
19
/**
16
20
* Class Customer
@@ -33,12 +37,12 @@ class CustomerManagement
33
37
protected $ accountManagement ;
34
38
35
39
/**
36
- * @var \Magento\Framework\Validator\Factory
40
+ * @var ValidatorFactory
37
41
*/
38
42
private $ validatorFactory ;
39
43
40
44
/**
41
- * @var \Magento\Customer\Model\ AddressFactory
45
+ * @var AddressFactory
42
46
*/
43
47
private $ addressFactory ;
44
48
@@ -47,23 +51,23 @@ class CustomerManagement
47
51
* @param CustomerRepository $customerRepository
48
52
* @param CustomerAddressRepository $customerAddressRepository
49
53
* @param AccountManagement $accountManagement
50
- * @param \Magento\Framework\Validator\Factory |null $validatorFactory
51
- * @param \Magento\Customer\Model\ AddressFactory|null $addressFactory
54
+ * @param ValidatorFactory |null $validatorFactory
55
+ * @param AddressFactory|null $addressFactory
52
56
*/
53
57
public function __construct (
54
58
CustomerRepository $ customerRepository ,
55
59
CustomerAddressRepository $ customerAddressRepository ,
56
60
AccountManagement $ accountManagement ,
57
- \ Magento \ Framework \ Validator \ Factory $ validatorFactory = null ,
58
- \ Magento \ Customer \ Model \ AddressFactory $ addressFactory = null
61
+ ValidatorFactory $ validatorFactory = null ,
62
+ AddressFactory $ addressFactory = null
59
63
) {
60
64
$ this ->customerRepository = $ customerRepository ;
61
65
$ this ->customerAddressRepository = $ customerAddressRepository ;
62
66
$ this ->accountManagement = $ accountManagement ;
63
67
$ this ->validatorFactory = $ validatorFactory ?: ObjectManager::getInstance ()
64
- ->get (\ Magento \ Framework \ Validator \Factory ::class);
68
+ ->get (ValidatorFactory ::class);
65
69
$ this ->addressFactory = $ addressFactory ?: ObjectManager::getInstance ()
66
- ->get (\ Magento \ Customer \ Model \ AddressFactory::class);
70
+ ->get (AddressFactory::class);
67
71
}
68
72
69
73
/**
@@ -82,6 +86,7 @@ public function populateCustomerInfo(QuoteEntity $quote)
82
86
$ quote ->getPasswordHash ()
83
87
);
84
88
$ quote ->setCustomer ($ customer );
89
+ $ this ->fillCustomerAddressId ($ quote );
85
90
}
86
91
if (!$ quote ->getBillingAddress ()->getId () && $ customer ->getDefaultBilling ()) {
87
92
$ quote ->getBillingAddress ()->importCustomerAddressData (
@@ -100,11 +105,36 @@ public function populateCustomerInfo(QuoteEntity $quote)
100
105
}
101
106
}
102
107
108
+ /**
109
+ * Filling 'CustomerAddressId' in quote for a newly created customer.
110
+ *
111
+ * @param QuoteEntity $quote
112
+ * @return void
113
+ */
114
+ private function fillCustomerAddressId (QuoteEntity $ quote ): void
115
+ {
116
+ $ customer = $ quote ->getCustomer ();
117
+
118
+ $ customer ->getDefaultBilling () ?
119
+ $ quote ->getBillingAddress ()->setCustomerAddressId ($ customer ->getDefaultBilling ()) :
120
+ $ quote ->getBillingAddress ()->setCustomerAddressId (0 );
121
+
122
+ if ($ customer ->getDefaultShipping () || $ customer ->getDefaultBilling ()) {
123
+ if ($ quote ->getShippingAddress ()->getSameAsBilling ()) {
124
+ $ quote ->getShippingAddress ()->setCustomerAddressId ($ customer ->getDefaultBilling ());
125
+ } else {
126
+ $ quote ->getShippingAddress ()->setCustomerAddressId ($ customer ->getDefaultShipping ());
127
+ }
128
+ } else {
129
+ $ quote ->getShippingAddress ()->setCustomerAddressId (0 );
130
+ }
131
+ }
132
+
103
133
/**
104
134
* Validate Quote Addresses
105
135
*
106
136
* @param Quote $quote
107
- * @throws \Magento\Framework\Validator\Exception
137
+ * @throws ValidatorException
108
138
* @return void
109
139
*/
110
140
public function validateAddresses (QuoteEntity $ quote )
@@ -126,7 +156,7 @@ public function validateAddresses(QuoteEntity $quote)
126
156
$ addressModel = $ this ->addressFactory ->create ();
127
157
$ addressModel ->updateData ($ address );
128
158
if (!$ validator ->isValid ($ addressModel )) {
129
- throw new \ Magento \ Framework \ Validator \ Exception (
159
+ throw new ValidatorException (
130
160
null ,
131
161
null ,
132
162
$ validator ->getMessages ()
0 commit comments