@@ -262,6 +262,32 @@ extension OrderDetailsViewModel {
262262 self . syncSubscriptions { _ in
263263 group. leave ( )
264264 }
265+
266+ // Shipping labels need to be synced after the order but before we complete
267+ // the order sync group to ensure the UI shows the latest data
268+ group. enter ( )
269+ Task { @MainActor [ weak self] in
270+ defer {
271+ group. leave ( )
272+ }
273+ // Check Woo Shipping support first, to ensure correct flows are enabled for shipping labels.
274+ self ? . dataSource. isEligibleForWooShipping = ( ( await self ? . isWooShippingSupported ( ) ) != nil )
275+
276+ // Check creation eligibility
277+ let isEligible = await self ? . checkShippingLabelCreationEligibility ( )
278+ self ? . dataSource. isEligibleForShippingLabelCreation = isEligible ?? false
279+
280+ // Sync shipping labels and update order with the result if available
281+ if let shippingLabels = await self ? . syncShippingLabels ( ) {
282+ // Update the order with the newly synced shipping labels
283+ if let updatedOrder = self ? . order. copy ( shippingLabels: shippingLabels) {
284+ self ? . update ( order: updatedOrder)
285+ }
286+ }
287+
288+ // Reload UI after shipping labels are synced
289+ onReloadSections ? ( )
290+ }
265291 }
266292
267293 group. enter ( )
@@ -282,22 +308,6 @@ extension OrderDetailsViewModel {
282308 onReloadSections ? ( )
283309 }
284310
285- group. enter ( )
286- Task { @MainActor in
287- defer {
288- onReloadSections ? ( )
289- group. leave ( )
290- }
291- // Check Woo Shipping support first, to ensure correct flows are enabled for shipping labels.
292- dataSource. isEligibleForWooShipping = await isWooShippingSupported ( )
293-
294- // Then sync shipping labels and check creation eligibility concurrently.
295- async let syncShippingLabels : ( ) = syncShippingLabels ( )
296- async let isEligibleForShippingLabelCreation = checkShippingLabelCreationEligibility ( )
297- _ = await syncShippingLabels
298- dataSource. isEligibleForShippingLabelCreation = await isEligibleForShippingLabelCreation
299- }
300-
301311 group. enter ( )
302312 syncSavedReceipts { _ in
303313 group. leave ( )
@@ -681,24 +691,24 @@ extension OrderDetailsViewModel {
681691 }
682692
683693 @MainActor
684- func syncShippingLabels( ) async {
694+ func syncShippingLabels( ) async -> [ ShippingLabel ] ? {
685695 guard await localRequirementsForShippingLabelsAreFulfilled ( ) else {
686- return
696+ return nil
687697 }
688698 return await withCheckedContinuation { continuation in
689699 stores. dispatch ( ShippingLabelAction . synchronizeShippingLabels ( siteID: order. siteID, orderID: order. orderID) { result in
690700 switch result {
691- case . success:
701+ case . success( let shippingLabels ) :
692702 ServiceLocator . analytics. track ( event: . shippingLabelsAPIRequest( result: . success) )
693- continuation. resume ( returning: ( ) )
703+ continuation. resume ( returning: shippingLabels )
694704 case . failure( let error) :
695705 ServiceLocator . analytics. track ( event: . shippingLabelsAPIRequest( result: . failed( error: error) ) )
696706 if error as? DotcomError == . noRestRoute {
697707 DDLogError ( " ⚠️ Endpoint for synchronizing shipping labels is unreachable. WC Shipping plugin may be missing. " )
698708 } else {
699709 DDLogError ( " ⛔️ Error synchronizing shipping labels: \( error) " )
700710 }
701- continuation. resume ( returning: ( ) )
711+ continuation. resume ( returning: nil )
702712 }
703713 } )
704714 }
0 commit comments