3737use Magento \Quote \Model \QuoteManagement ;
3838use ShipperHQ \Shipper \Helper \CarrierGroup ;
3939use ShipperHQ \Shipper \Helper \LogAssist ;
40+ use ShipperHQ \Shipper \Model \ResourceModel \Quote \AddressDetail \CollectionFactory as AddressDetailCollectionFactory ;
4041
4142class QuoteManagementPlugin
4243{
@@ -65,18 +66,25 @@ class QuoteManagementPlugin
6566 */
6667 private $ logger ;
6768
69+ /**
70+ * @var AddressDetailCollectionFactory
71+ */
72+ private $ addressDetailCollectionFactory ;
73+
6874 public function __construct (
6975 CartRepositoryInterface $ quoteRepository ,
7076 ManagerInterface $ eventManager ,
7177 DataObjectFactory $ objectFactory ,
7278 CarrierGroup $ carrierGroupHelper ,
73- LogAssist $ logger
79+ LogAssist $ logger ,
80+ AddressDetailCollectionFactory $ addressDetailCollectionFactory
7481 ) {
7582 $ this ->quoteRepository = $ quoteRepository ;
7683 $ this ->eventManager = $ eventManager ;
7784 $ this ->objectFactory = $ objectFactory ;
7885 $ this ->carrierGroupHelper = $ carrierGroupHelper ;
7986 $ this ->logger = $ logger ;
87+ $ this ->addressDetailCollectionFactory = $ addressDetailCollectionFactory ;
8088 }
8189
8290 /**
@@ -115,11 +123,17 @@ private function updateCarrierGroupDetails(int $cartId): void
115123 if (!$ address ) {
116124 return ;
117125 }
126+
118127 $ shippingMethod = (string ) $ address ->getShippingMethod ();
119128 if (!$ shippingMethod ) {
120129 return ;
121130 }
122131
132+ // Extension attributes have been wiped by this point as they're just held in memory and Magento
133+ // has reloaded the quote address from the DB now. Let's get them from shipperhq_quote_address_detail
134+ // which has been updated at this point to have the correct values
135+ $ addressExtensionAttributes = $ this ->getQuoteAddressDetails ($ address ->getId ());
136+
123137 $ additionalDetail = $ this ->objectFactory ->create ();
124138 $ carrierCode = '' ;
125139 if (strpos ($ shippingMethod , '_ ' ) !== false ) {
@@ -130,7 +144,7 @@ private function updateCarrierGroupDetails(int $cartId): void
130144 $ this ->eventManager ->dispatch (
131145 'shipperhq_additional_detail_checkout ' ,
132146 [
133- 'address_extn_attributes ' => $ address -> getExtensionAttributes () ,
147+ 'address_extn_attributes ' => $ addressExtensionAttributes ,
134148 'additional_detail ' => $ additionalDetail ,
135149 'carrier_code ' => $ carrierCode ,
136150 'address ' => $ address ,
@@ -143,6 +157,8 @@ private function updateCarrierGroupDetails(int $cartId): void
143157 $ shippingMethod ,
144158 $ additionalDetail ->convertToArray ()
145159 );
160+
161+
146162 } catch (\Throwable $ e ) {
147163 $ this ->logger ->postCritical (
148164 'Shipperhq_Shipper ' ,
@@ -151,4 +167,44 @@ private function updateCarrierGroupDetails(int $cartId): void
151167 );
152168 }
153169 }
170+
171+ /**
172+ * Get quote address details for a given address ID and extract what would have been extension attributes
173+ *
174+ * @param int $addressId
175+ * @return \Magento\Framework\DataObject
176+ */
177+ private function getQuoteAddressDetails (int $ addressId )
178+ {
179+ $ collection = $ this ->addressDetailCollectionFactory ->create ()
180+ ->addAddressToFilter ($ addressId );
181+
182+ $ extensionAttributes = $ this ->objectFactory ->create ();
183+
184+ foreach ($ collection as $ detail ) {
185+ if ($ detail ->getDeliveryDate ()) {
186+ $ extensionAttributes ->setDeliveryDate ($ detail ->getDeliveryDate ());
187+
188+ if ($ detail ->getTimeSlot ()) {
189+ $ extensionAttributes ->setTimeSlot ($ detail ->getTimeSlot ());
190+ }
191+ }
192+
193+ if ($ detail ->getPickupLocationId ()) {
194+ $ extensionAttributes ->setLocationId ($ detail ->getPickupLocationId ());
195+ }
196+
197+ if ($ detail ->getPickupLocation ()) {
198+ $ extensionAttributes ->setLocationAddress ($ detail ->getPickupLocation ());
199+ }
200+
201+ $ this ->logger ->postDebug (
202+ 'Shipperhq_Shipper ' ,
203+ 'Extension Attributes Extracted from Address Detail ' ,
204+ $ extensionAttributes ->getData ()
205+ );
206+ }
207+
208+ return $ extensionAttributes ;
209+ }
154210}
0 commit comments