33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Quote \Model ;
89
9- use Magento \Customer \Api \CustomerRepositoryInterface as CustomerRepository ;
1010use Magento \Customer \Api \AccountManagementInterface as AccountManagement ;
1111use 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 ;
1314use 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 ;
1418
1519/**
1620 * Class Customer
@@ -33,12 +37,12 @@ class CustomerManagement
3337 protected $ accountManagement ;
3438
3539 /**
36- * @var \Magento\Framework\Validator\Factory
40+ * @var ValidatorFactory
3741 */
3842 private $ validatorFactory ;
3943
4044 /**
41- * @var \Magento\Customer\Model\ AddressFactory
45+ * @var AddressFactory
4246 */
4347 private $ addressFactory ;
4448
@@ -47,23 +51,23 @@ class CustomerManagement
4751 * @param CustomerRepository $customerRepository
4852 * @param CustomerAddressRepository $customerAddressRepository
4953 * @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
5256 */
5357 public function __construct (
5458 CustomerRepository $ customerRepository ,
5559 CustomerAddressRepository $ customerAddressRepository ,
5660 AccountManagement $ accountManagement ,
57- \ Magento \ Framework \ Validator \ Factory $ validatorFactory = null ,
58- \ Magento \ Customer \ Model \ AddressFactory $ addressFactory = null
61+ ValidatorFactory $ validatorFactory = null ,
62+ AddressFactory $ addressFactory = null
5963 ) {
6064 $ this ->customerRepository = $ customerRepository ;
6165 $ this ->customerAddressRepository = $ customerAddressRepository ;
6266 $ this ->accountManagement = $ accountManagement ;
6367 $ this ->validatorFactory = $ validatorFactory ?: ObjectManager::getInstance ()
64- ->get (\ Magento \ Framework \ Validator \Factory ::class);
68+ ->get (ValidatorFactory ::class);
6569 $ this ->addressFactory = $ addressFactory ?: ObjectManager::getInstance ()
66- ->get (\ Magento \ Customer \ Model \ AddressFactory::class);
70+ ->get (AddressFactory::class);
6771 }
6872
6973 /**
@@ -82,6 +86,7 @@ public function populateCustomerInfo(QuoteEntity $quote)
8286 $ quote ->getPasswordHash ()
8387 );
8488 $ quote ->setCustomer ($ customer );
89+ $ this ->fillCustomerAddressId ($ quote );
8590 }
8691 if (!$ quote ->getBillingAddress ()->getId () && $ customer ->getDefaultBilling ()) {
8792 $ quote ->getBillingAddress ()->importCustomerAddressData (
@@ -100,11 +105,36 @@ public function populateCustomerInfo(QuoteEntity $quote)
100105 }
101106 }
102107
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+
103133 /**
104134 * Validate Quote Addresses
105135 *
106136 * @param Quote $quote
107- * @throws \Magento\Framework\Validator\Exception
137+ * @throws ValidatorException
108138 * @return void
109139 */
110140 public function validateAddresses (QuoteEntity $ quote )
@@ -126,7 +156,7 @@ public function validateAddresses(QuoteEntity $quote)
126156 $ addressModel = $ this ->addressFactory ->create ();
127157 $ addressModel ->updateData ($ address );
128158 if (!$ validator ->isValid ($ addressModel )) {
129- throw new \ Magento \ Framework \ Validator \ Exception (
159+ throw new ValidatorException (
130160 null ,
131161 null ,
132162 $ validator ->getMessages ()
0 commit comments