@@ -2,7 +2,6 @@ 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' ;
65import {
76 useDraftOrder ,
87 useDraftOrderShipping ,
@@ -74,21 +73,20 @@ export function ShippingMethodForm() {
7473 } ) ;
7574
7675 const applyShippingMethod = useApplyShippingMethod ( ) ;
77- const applyDeliveryMethod = useApplyDeliveryMethod ( ) ;
7876
7977 // Track the last processed state to avoid duplicate API calls
8078 const lastProcessedStateRef = useRef < {
8179 serviceCode : string | null ;
8280 cost : number | null ;
8381 hadShippingMethods : boolean ;
8482 wasPickup : boolean ;
85- appliedDeliveryMethod : boolean ;
83+ clearedShippingMethod : boolean ;
8684 } > ( {
8785 serviceCode : null ,
8886 cost : null ,
8987 hadShippingMethods : false ,
9088 wasPickup : false ,
91- appliedDeliveryMethod : false ,
89+ clearedShippingMethod : false ,
9290 } ) ;
9391
9492 useEffect ( ( ) => {
@@ -99,36 +97,24 @@ export function ShippingMethodForm() {
9997 const currentCost = shippingLines ?. amount ?. value ?? null ;
10098 const lastState = lastProcessedStateRef . current ;
10199
102- // Case 1: No shipping methods available
100+ // Case 1: No shipping methods available - clear shipping and set fulfillment to SHIP
103101 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- }
102+ // Apply empty shipping method if:
103+ // - Pickup mode and has shipping code OR wasn't pickup before
104+ // - Shipping mode and (had methods before OR haven't cleared yet)
105+ const shouldClearShipping = isPickup
106+ ? currentServiceCode || ! lastState . wasPickup
107+ : lastState . hadShippingMethods || ! lastState . clearedShippingMethod ;
117108
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- ) {
109+ if ( shouldClearShipping ) {
124110 form . setValue ( 'shippingMethod' , '' , { shouldDirty : false } ) ;
125- applyDeliveryMethod . mutate ( DeliveryMethods . SHIP ) ;
111+ applyShippingMethod . mutate ( [ ] ) ;
126112 lastProcessedStateRef . current = {
127113 serviceCode : null ,
128114 cost : null ,
129115 hadShippingMethods : false ,
130- wasPickup : false ,
131- appliedDeliveryMethod : true ,
116+ wasPickup : isPickup ,
117+ clearedShippingMethod : true ,
132118 } ;
133119 }
134120 return ;
@@ -182,7 +168,7 @@ export function ShippingMethodForm() {
182168 cost : methodCost ,
183169 hadShippingMethods : true ,
184170 wasPickup : false ,
185- appliedDeliveryMethod : false ,
171+ clearedShippingMethod : false ,
186172 } ;
187173 }
188174 }
@@ -193,7 +179,6 @@ export function ShippingMethodForm() {
193179 isShippingMethodsLoading ,
194180 form ,
195181 applyShippingMethod ,
196- applyDeliveryMethod ,
197182 updateTaxes . mutate ,
198183 session ?. enableTaxCollection ,
199184 isPickup ,
0 commit comments