-
Notifications
You must be signed in to change notification settings - Fork 214
@W-18902385 - [BOPIS][Bug] Fix prefilling shipping address bug #2673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,7 @@ const ProductDetail = () => { | |
| const {data: basket, isLoading: isBasketLoading} = useCurrentBasket() | ||
| const {addItemToNewOrExistingBasket} = useShopperBasketsMutationHelper() | ||
| const updateItemsInBasketMutation = useShopperBasketsMutation('updateItemsInBasket') | ||
| const updateShipmentForBasketMutation = useShopperBasketsMutation('updateShipmentForBasket') | ||
| const {res} = useServerContext() | ||
| if (res) { | ||
| res.set( | ||
|
|
@@ -344,6 +345,66 @@ const ProductDetail = () => { | |
|
|
||
| const addToCartModal = useAddToCartModalContext() | ||
|
|
||
| /** | ||
| * Helper function to configure shipping method based on pickup selection | ||
| * @param {Object} basketResponse - The basket response from adding items | ||
| * @param {Array} productItems - Array of product items that were added | ||
| * @param {boolean} hasAnyPickupSelected - Whether any items have pickup selected | ||
| * @returns {Promise<void>} | ||
| */ | ||
| const configureShippingMethodIfNeeded = async ( | ||
|
||
| basketResponse, | ||
| productItems, | ||
| hasAnyPickupSelected | ||
| ) => { | ||
| if (!basketResponse?.basketId || !basketResponse.shipments.length) { | ||
| return | ||
| } | ||
|
|
||
| const currentShippingMethod = basketResponse.shipments[0].shippingMethod | ||
| const isCurrentlyPickup = isCurrentShippingMethodPickup(currentShippingMethod) | ||
|
|
||
| // Only configure if there's a mismatch between pickup selection and current method | ||
| if ( | ||
| (hasAnyPickupSelected && !isCurrentlyPickup) || | ||
| (!hasAnyPickupSelected && isCurrentlyPickup) | ||
| ) { | ||
| // Clear shipping address when there's a mismatch by updating shipment without shippingAddress | ||
| await updateShipmentForBasketMutation.mutateAsync({ | ||
| parameters: { | ||
| basketId: basketResponse.basketId, | ||
| shipmentId: 'me' | ||
| }, | ||
| body: { | ||
| shippingAddress: {} | ||
| } | ||
|
||
| }) | ||
|
|
||
| // Fetch shipping methods to get available options | ||
| const {data: fetchedShippingMethods} = await refetchShippingMethods() | ||
|
|
||
| if (hasAnyPickupSelected && !isCurrentlyPickup) { | ||
| // Configure pickup shipment if pickup is selected but current method is not pickup | ||
| const pickupShippingMethodId = getPickupShippingMethodId(fetchedShippingMethods) | ||
| await configurePickupShipment( | ||
| basketResponse.basketId, | ||
| productItems, | ||
| selectedStore, | ||
| { | ||
| pickupShippingMethodId | ||
| } | ||
| ) | ||
| } else if (!hasAnyPickupSelected && isCurrentlyPickup) { | ||
| // Configure regular shipping if pickup is not selected but current method is pickup | ||
| const defaultShippingMethodId = getDefaultShippingMethodId(fetchedShippingMethods) | ||
| await configureRegularShippingMethod( | ||
| basketResponse.basketId, | ||
| defaultShippingMethodId | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const handleAddToCart = async (productSelectionValues = []) => { | ||
| try { | ||
| let productItems = productSelectionValues.map((item) => { | ||
|
|
@@ -415,37 +476,11 @@ const ProductDetail = () => { | |
| const basketResponse = await addItemToNewOrExistingBasket(productItems) | ||
|
|
||
| // Configure shipping method based on pickup selection | ||
| if (basketResponse?.basketId && basketResponse.shipments.length > 0) { | ||
| const currentShippingMethod = basketResponse.shipments[0].shippingMethod | ||
| const isCurrentlyPickup = isCurrentShippingMethodPickup(currentShippingMethod) | ||
|
|
||
| if (hasAnyPickupSelected && !isCurrentlyPickup) { | ||
| // Fetch shipping methods to get available options | ||
| const {data: fetchedShippingMethods} = await refetchShippingMethods() | ||
|
|
||
| // Configure pickup shipment if pickup is selected but current method is not pickup | ||
| const pickupShippingMethodId = getPickupShippingMethodId(fetchedShippingMethods) | ||
| await configurePickupShipment( | ||
| basketResponse.basketId, | ||
| productItems, | ||
| selectedStore, | ||
| { | ||
| pickupShippingMethodId | ||
| } | ||
| ) | ||
| } else if (!hasAnyPickupSelected && isCurrentlyPickup) { | ||
| // Fetch shipping methods to get available options | ||
| const {data: fetchedShippingMethods} = await refetchShippingMethods() | ||
|
|
||
| // Configure regular shipping if pickup is not selected but current method is pickup | ||
| const defaultShippingMethodId = | ||
| getDefaultShippingMethodId(fetchedShippingMethods) | ||
| await configureRegularShippingMethod( | ||
| basketResponse.basketId, | ||
| defaultShippingMethodId | ||
| ) | ||
| } | ||
| } | ||
| await configureShippingMethodIfNeeded( | ||
| basketResponse, | ||
| productItems, | ||
| hasAnyPickupSelected | ||
| ) | ||
|
|
||
| const productItemsForEinstein = productSelectionValues.map( | ||
| ({product, variant, quantity}) => ({ | ||
|
|
@@ -626,26 +661,7 @@ const ProductDetail = () => { | |
| } | ||
|
|
||
| // Configure shipping method based on pickup selection | ||
| if (res.basketId && res.shipments.length > 0) { | ||
| const currentShippingMethod = res.shipments[0].shippingMethod | ||
| const isCurrentlyPickup = isCurrentShippingMethodPickup(currentShippingMethod) | ||
|
|
||
| // Fetch shipping methods to get available options | ||
| const {data: fetchedShippingMethods} = await refetchShippingMethods() | ||
|
|
||
| if (hasAnyPickupSelected && !isCurrentlyPickup) { | ||
| // Configure pickup shipment if pickup is selected but current method is not pickup | ||
| const pickupShippingMethodId = getPickupShippingMethodId(fetchedShippingMethods) | ||
| await configurePickupShipment(res.basketId, productItems, selectedStore, { | ||
| pickupShippingMethodId | ||
| }) | ||
| } else if (!hasAnyPickupSelected && isCurrentlyPickup) { | ||
| // Configure regular shipping if pickup is not selected but current method is pickup | ||
| const defaultShippingMethodId = | ||
| getDefaultShippingMethodId(fetchedShippingMethods) | ||
| await configureRegularShippingMethod(res.basketId, defaultShippingMethodId) | ||
| } | ||
| } | ||
| await configureShippingMethodIfNeeded(res, productItems, hasAnyPickupSelected) | ||
|
|
||
| einstein.sendAddToCart(productItems) | ||
| // Open modal with itemsAdded and selectedQuantity for bundles | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this i18n safe to assume addresses have a street and city?