@@ -2,6 +2,7 @@ import { useEffect, useRef } from 'react';
22import { useFormContext } from 'react-hook-form' ;
33import { useCheckoutContext } from '@/components/checkout/checkout' ;
44import { DeliveryMethods } from '@/components/checkout/delivery/delivery-method' ;
5+ import { useApplyDeliveryMethod } from '@/components/checkout/delivery/utils/use-apply-delivery-method' ;
56import {
67 useDraftOrder ,
78 useDraftOrderShipping ,
@@ -73,18 +74,21 @@ export function ShippingMethodForm() {
7374 } ) ;
7475
7576 const applyShippingMethod = useApplyShippingMethod ( ) ;
77+ const applyDeliveryMethod = useApplyDeliveryMethod ( ) ;
7678
7779 // Track the last processed state to avoid duplicate API calls
7880 const lastProcessedStateRef = useRef < {
7981 serviceCode : string | null ;
8082 cost : number | null ;
8183 hadShippingMethods : boolean ;
8284 wasPickup : boolean ;
85+ appliedDeliveryMethod : boolean ;
8386 } > ( {
8487 serviceCode : null ,
8588 cost : null ,
8689 hadShippingMethods : false ,
8790 wasPickup : false ,
91+ appliedDeliveryMethod : false ,
8892 } ) ;
8993
9094 useEffect ( ( ) => {
@@ -95,22 +99,38 @@ export function ShippingMethodForm() {
9599 const currentCost = shippingLines ?. amount ?. value ?? null ;
96100 const lastState = lastProcessedStateRef . current ;
97101
98- // Case 1: No shipping methods available but shipping line exists - clear it
99- // Only clear once when transitioning from having methods to no methods, or when switching to pickup
100- if (
101- ! hasShippingMethods &&
102- hasShippingAddress &&
103- ( ( currentServiceCode && lastState . hadShippingMethods ) ||
104- ( isPickup && ! lastState . wasPickup ) )
105- ) {
106- form . setValue ( 'shippingMethod' , '' , { shouldDirty : false } ) ;
107- applyShippingMethod . mutate ( [ ] ) ;
108- lastProcessedStateRef . current = {
109- serviceCode : null ,
110- cost : null ,
111- hadShippingMethods : false ,
112- wasPickup : isPickup ,
113- } ;
102+ // Case 1: No shipping methods available
103+ if ( ! hasShippingMethods && hasShippingAddress ) {
104+ // If pickup mode, clear the shipping method
105+ if ( isPickup && ( currentServiceCode || ! lastState . wasPickup ) ) {
106+ form . setValue ( 'shippingMethod' , '' , { shouldDirty : false } ) ;
107+ applyShippingMethod . mutate ( [ ] ) ;
108+ lastProcessedStateRef . current = {
109+ serviceCode : null ,
110+ cost : null ,
111+ hadShippingMethods : false ,
112+ wasPickup : true ,
113+ appliedDeliveryMethod : false ,
114+ } ;
115+ return ;
116+ }
117+
118+ // If shipping mode with no methods, apply SHIP delivery method
119+ // Apply if: transitioning from having methods OR haven't applied it yet
120+ if (
121+ ! isPickup &&
122+ ( lastState . hadShippingMethods || ! lastState . appliedDeliveryMethod )
123+ ) {
124+ form . setValue ( 'shippingMethod' , '' , { shouldDirty : false } ) ;
125+ applyDeliveryMethod . mutate ( DeliveryMethods . SHIP ) ;
126+ lastProcessedStateRef . current = {
127+ serviceCode : null ,
128+ cost : null ,
129+ hadShippingMethods : false ,
130+ wasPickup : false ,
131+ appliedDeliveryMethod : true ,
132+ } ;
133+ }
114134 return ;
115135 }
116136
@@ -162,6 +182,7 @@ export function ShippingMethodForm() {
162182 cost : methodCost ,
163183 hadShippingMethods : true ,
164184 wasPickup : false ,
185+ appliedDeliveryMethod : false ,
165186 } ;
166187 }
167188 }
@@ -172,6 +193,7 @@ export function ShippingMethodForm() {
172193 isShippingMethodsLoading ,
173194 form ,
174195 applyShippingMethod ,
196+ applyDeliveryMethod ,
175197 updateTaxes . mutate ,
176198 session ?. enableTaxCollection ,
177199 isPickup ,
0 commit comments