1313use Magento \Framework \Exception \LocalizedException ;
1414use Magento \Framework \Exception \StateException ;
1515use Magento \Quote \Api \Data \PaymentInterface ;
16- use Magento \Quote \Model \Quote \Address ;
1716use Magento \Quote \Model \Quote \Address \ToOrder as ToOrderConverter ;
1817use Magento \Quote \Model \Quote \Address \ToOrderAddress as ToOrderAddressConverter ;
1918use Magento \Quote \Model \Quote as QuoteEntity ;
@@ -136,6 +135,16 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
136135 */
137136 private $ quoteIdMaskFactory ;
138137
138+ /**
139+ * @var \Magento\Customer\Api\AddressRepositoryInterface
140+ */
141+ private $ addressRepository ;
142+
143+ /**
144+ * @var array
145+ */
146+ private $ addressesToSync = [];
147+
139148 /**
140149 * @param EventManager $eventManager
141150 * @param QuoteValidator $quoteValidator
@@ -158,6 +167,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
158167 * @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
159168 * @param QuoteFactory $quoteFactory
160169 * @param \Magento\Quote\Model\QuoteIdMaskFactory|null $quoteIdMaskFactory
170+ * @param \Magento\Customer\Api\AddressRepositoryInterface|null $addressRepository
161171 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
162172 */
163173 public function __construct (
@@ -181,7 +191,8 @@ public function __construct(
181191 \Magento \Customer \Model \Session $ customerSession ,
182192 \Magento \Customer \Api \AccountManagementInterface $ accountManagement ,
183193 \Magento \Quote \Model \QuoteFactory $ quoteFactory ,
184- \Magento \Quote \Model \QuoteIdMaskFactory $ quoteIdMaskFactory = null
194+ \Magento \Quote \Model \QuoteIdMaskFactory $ quoteIdMaskFactory = null ,
195+ \Magento \Customer \Api \AddressRepositoryInterface $ addressRepository = null
185196 ) {
186197 $ this ->eventManager = $ eventManager ;
187198 $ this ->quoteValidator = $ quoteValidator ;
@@ -205,6 +216,8 @@ public function __construct(
205216 $ this ->quoteFactory = $ quoteFactory ;
206217 $ this ->quoteIdMaskFactory = $ quoteIdMaskFactory ?: ObjectManager::getInstance ()
207218 ->get (\Magento \Quote \Model \QuoteIdMaskFactory::class);
219+ $ this ->addressRepository = $ addressRepository ?: ObjectManager::getInstance ()
220+ ->get (\Magento \Customer \Api \AddressRepositoryInterface::class);
208221 }
209222
210223 /**
@@ -428,6 +441,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
428441 if (!$ quote ->getCustomerIsGuest ()) {
429442 if ($ quote ->getCustomerId ()) {
430443 $ this ->_prepareCustomerQuote ($ quote );
444+ $ this ->customerManagement ->validateAddresses ($ quote );
431445 }
432446 $ this ->customerManagement ->populateCustomerInfo ($ quote );
433447 }
@@ -496,6 +510,11 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
496510 );
497511 $ this ->quoteRepository ->save ($ quote );
498512 } catch (\Exception $ e ) {
513+ if (!empty ($ this ->addressesToSync )) {
514+ foreach ($ this ->addressesToSync as $ addressId ) {
515+ $ this ->addressRepository ->deleteById ($ addressId );
516+ }
517+ }
499518 $ this ->eventManager ->dispatch (
500519 'sales_model_service_quote_submit_failure ' ,
501520 [
@@ -536,8 +555,12 @@ protected function _prepareCustomerQuote($quote)
536555 $ shippingAddress ->setIsDefaultShipping (true );
537556 $ hasDefaultShipping = true ;
538557 }
558+ //save here new customer address
559+ $ shippingAddress ->setCustomerId ($ quote ->getCustomerId ());
560+ $ this ->addressRepository ->save ($ shippingAddress );
539561 $ quote ->addCustomerAddress ($ shippingAddress );
540562 $ shipping ->setCustomerAddressData ($ shippingAddress );
563+ $ this ->addressesToSync [] = $ shippingAddress ->getId ();
541564 $ shipping ->setCustomerAddressId ($ shippingAddress ->getId ());
542565 }
543566
@@ -551,8 +574,11 @@ protected function _prepareCustomerQuote($quote)
551574 }
552575 $ billingAddress ->setIsDefaultBilling (true );
553576 }
577+ $ billingAddress ->setCustomerId ($ quote ->getCustomerId ());
578+ $ this ->addressRepository ->save ($ billingAddress );
554579 $ quote ->addCustomerAddress ($ billingAddress );
555580 $ billing ->setCustomerAddressData ($ billingAddress );
581+ $ this ->addressesToSync [] = $ billingAddress ->getId ();
556582 $ billing ->setCustomerAddressId ($ billingAddress ->getId ());
557583 }
558584 if ($ shipping && !$ shipping ->getCustomerId () && !$ hasDefaultBilling ) {
0 commit comments