Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-ends-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@godaddy/react": patch
---

Adjust shipping intent logic to clear shippingLines when no new shipping methods are returned
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -74,21 +73,20 @@ export function ShippingMethodForm() {
});

const applyShippingMethod = useApplyShippingMethod();
const applyDeliveryMethod = useApplyDeliveryMethod();

// Track the last processed state to avoid duplicate API calls
const lastProcessedStateRef = useRef<{
serviceCode: string | null;
cost: number | null;
hadShippingMethods: boolean;
wasPickup: boolean;
appliedDeliveryMethod: boolean;
clearedShippingMethod: boolean;
}>({
serviceCode: null,
cost: null,
hadShippingMethods: false,
wasPickup: false,
appliedDeliveryMethod: false,
clearedShippingMethod: false,
});

useEffect(() => {
Expand All @@ -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;
Expand Down Expand Up @@ -182,7 +168,7 @@ export function ShippingMethodForm() {
cost: methodCost,
hadShippingMethods: true,
wasPickup: false,
appliedDeliveryMethod: false,
clearedShippingMethod: false,
};
}
}
Expand All @@ -193,7 +179,6 @@ export function ShippingMethodForm() {
isShippingMethodsLoading,
form,
applyShippingMethod,
applyDeliveryMethod,
updateTaxes.mutate,
session?.enableTaxCollection,
isPickup,
Expand Down