diff --git a/.changeset/rich-ends-film.md b/.changeset/rich-ends-film.md new file mode 100644 index 00000000..dc824c00 --- /dev/null +++ b/.changeset/rich-ends-film.md @@ -0,0 +1,5 @@ +--- +"@godaddy/react": patch +--- + +Adjust shipping intent logic to clear shippingLines when no new shipping methods are returned diff --git a/packages/react/src/components/checkout/shipping/shipping-method.tsx b/packages/react/src/components/checkout/shipping/shipping-method.tsx index 9d68c82a..1bbbdfc0 100644 --- a/packages/react/src/components/checkout/shipping/shipping-method.tsx +++ b/packages/react/src/components/checkout/shipping/shipping-method.tsx @@ -2,7 +2,6 @@ import { useEffect, useRef } from 'react'; import { useFormContext } from 'react-hook-form'; import { useCheckoutContext } from '@/components/checkout/checkout'; import { DeliveryMethods } from '@/components/checkout/delivery/delivery-method'; -import { useApplyDeliveryMethod } from '@/components/checkout/delivery/utils/use-apply-delivery-method'; import { useDraftOrder, useDraftOrderShipping, @@ -74,7 +73,6 @@ export function ShippingMethodForm() { }); const applyShippingMethod = useApplyShippingMethod(); - const applyDeliveryMethod = useApplyDeliveryMethod(); // Track the last processed state to avoid duplicate API calls const lastProcessedStateRef = useRef<{ @@ -82,13 +80,13 @@ export function ShippingMethodForm() { cost: number | null; hadShippingMethods: boolean; wasPickup: boolean; - appliedDeliveryMethod: boolean; + clearedShippingMethod: boolean; }>({ serviceCode: null, cost: null, hadShippingMethods: false, wasPickup: false, - appliedDeliveryMethod: false, + clearedShippingMethod: false, }); useEffect(() => { @@ -99,36 +97,24 @@ export function ShippingMethodForm() { const currentCost = shippingLines?.amount?.value ?? null; const lastState = lastProcessedStateRef.current; - // Case 1: No shipping methods available + // Case 1: No shipping methods available - clear shipping and set fulfillment to SHIP if (!hasShippingMethods && hasShippingAddress) { - // If pickup mode, clear the shipping method - if (isPickup && (currentServiceCode || !lastState.wasPickup)) { - form.setValue('shippingMethod', '', { shouldDirty: false }); - applyShippingMethod.mutate([]); - lastProcessedStateRef.current = { - serviceCode: null, - cost: null, - hadShippingMethods: false, - wasPickup: true, - appliedDeliveryMethod: false, - }; - return; - } + // Apply empty shipping method if: + // - Pickup mode and has shipping code OR wasn't pickup before + // - Shipping mode and (had methods before OR haven't cleared yet) + const shouldClearShipping = isPickup + ? currentServiceCode || !lastState.wasPickup + : lastState.hadShippingMethods || !lastState.clearedShippingMethod; - // If shipping mode with no methods, apply SHIP delivery method - // Apply if: transitioning from having methods OR haven't applied it yet - if ( - !isPickup && - (lastState.hadShippingMethods || !lastState.appliedDeliveryMethod) - ) { + if (shouldClearShipping) { form.setValue('shippingMethod', '', { shouldDirty: false }); - applyDeliveryMethod.mutate(DeliveryMethods.SHIP); + applyShippingMethod.mutate([]); lastProcessedStateRef.current = { serviceCode: null, cost: null, hadShippingMethods: false, - wasPickup: false, - appliedDeliveryMethod: true, + wasPickup: isPickup, + clearedShippingMethod: true, }; } return; @@ -182,7 +168,7 @@ export function ShippingMethodForm() { cost: methodCost, hadShippingMethods: true, wasPickup: false, - appliedDeliveryMethod: false, + clearedShippingMethod: false, }; } } @@ -193,7 +179,6 @@ export function ShippingMethodForm() { isShippingMethodsLoading, form, applyShippingMethod, - applyDeliveryMethod, updateTaxes.mutate, session?.enableTaxCollection, isPickup,