11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2022 Adobe
4+ * All Rights Reserved .
55 */
66declare (strict_types=1 );
77
@@ -86,16 +86,8 @@ public function clearAddressItem($subject, $request)
8686 $ isMultipleShippingAddressesPresent = $ quote ->isMultipleShippingAddresses ();
8787 if ($ isMultipleShippingAddressesPresent || $ this ->isDisableMultishippingRequired ($ request , $ quote )) {
8888 $ this ->disableMultishipping ->execute ($ quote );
89- foreach ($ quote ->getAllShippingAddresses () as $ address ) {
90- $ quote ->removeAddress ($ address ->getId ());
91- }
89+ $ this ->setDefaultShippingAddress ($ quote );
9290
93- $ shippingAddress = $ quote ->getShippingAddress ();
94- $ defaultShipping = $ quote ->getCustomer ()->getDefaultShipping ();
95- if ($ defaultShipping ) {
96- $ defaultCustomerAddress = $ this ->addressRepository ->getById ($ defaultShipping );
97- $ shippingAddress ->importCustomerAddressData ($ defaultCustomerAddress );
98- }
9991 if ($ isMultipleShippingAddressesPresent ) {
10092 $ this ->checkoutSession ->setMultiShippingAddressesFlag (true );
10193 }
@@ -111,6 +103,35 @@ public function clearAddressItem($subject, $request)
111103 }
112104 }
113105
106+ /**
107+ * Determine shipping address from current multi-shipping configuration
108+ *
109+ * @param Quote $quote
110+ * @return void
111+ * @throws LocalizedException
112+ */
113+ private function setDefaultShippingAddress (Quote $ quote ): void
114+ {
115+ $ currentShippingAddress = $ quote ->getShippingAddress ();
116+ foreach ($ quote ->getAllShippingAddresses () as $ address ) {
117+ if ($ address ->getId () === $ currentShippingAddress ->getId ()) {
118+ continue ;
119+ }
120+ $ quote ->removeAddress ($ address ->getId ());
121+ }
122+
123+ if ($ currentShippingAddress ) {
124+ $ quote ->addShippingAddress ($ currentShippingAddress );
125+ } else {
126+ $ shippingAddress = $ quote ->getShippingAddress ();
127+ $ defaultShipping = $ quote ->getCustomer ()->getDefaultShipping ();
128+ if ($ defaultShipping ) {
129+ $ defaultCustomerAddress = $ this ->addressRepository ->getById ($ defaultShipping );
130+ $ shippingAddress ->importCustomerAddressData ($ defaultCustomerAddress );
131+ }
132+ }
133+ }
134+
114135 /**
115136 * Checks whether quote has virtual items
116137 *
0 commit comments