Skip to content

Commit baa0359

Browse files
@W-20002879 Register guest save addresses is multi-ship aware (#3412)
1 parent 1688036 commit baa0359

File tree

3 files changed

+125
-11
lines changed

3 files changed

+125
-11
lines changed

packages/template-retail-react-app/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- Enhanced the shopping assistant that integrates Salesforce Embedded Messaging Service with PWA Kit applications, adding comprehensive context support, localization capabilities, and improved user experience features. [#3259](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3259)
1010
- Removed domainUrl, locale, basetId properties as part off the ShopperAgent during initialization. [#3259](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3259)
1111
- Only show option to deliver to multiple addresses if there are multiple items in the basket. [#3336](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3336)
12+
- When registering a guest user on the confirmation page only save the delivery addresses to the new account
13+
[#3412](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3412)
1214
- Added support for Choice of Bonus Products feature. Users can now select from available bonus products when they qualify for the associated promotion. The bonus product selection flow can be entered from either the "Item Added to Cart" modal (when adding the qualifying product to the cart) or from the cart page. [#3292] (https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3292)
1315
- Add @h4ad/serverless-adapter to jest config [#3325](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3325)
1416
- Fix bug where pick up items were displaying delivery stock levels instead of in store stock levels [#3401](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3401)

packages/template-retail-react-app/app/pages/checkout/confirmation.jsx

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import {
3131
useShopperCustomersMutation
3232
} from '@salesforce/commerce-sdk-react'
3333
import {getCreditCardIcon} from '@salesforce/retail-react-app/app/utils/cc-utils'
34+
import {getConfig} from '@salesforce/pwa-kit-runtime/utils/ssr-config'
35+
import {isPickupShipment} from '@salesforce/retail-react-app/app/utils/shipment-utils'
36+
import {areAddressesEqual} from '@salesforce/retail-react-app/app/utils/address-utils'
3437

3538
// Components
3639
import Link from '@salesforce/retail-react-app/app/components/link'
@@ -51,7 +54,10 @@ import {useCurrentCustomer} from '@salesforce/retail-react-app/app/hooks/use-cur
5154
import {useCurrency} from '@salesforce/retail-react-app/app/hooks'
5255

5356
// Constants
54-
import {API_ERROR_MESSAGE} from '@salesforce/retail-react-app/app/constants'
57+
import {
58+
API_ERROR_MESSAGE,
59+
STORE_LOCATOR_IS_ENABLED
60+
} from '@salesforce/retail-react-app/app/constants'
5561

5662
const onClient = typeof window !== 'undefined'
5763

@@ -96,19 +102,42 @@ const CheckoutConfirmation = () => {
96102
const CardIcon = getCreditCardIcon(order.paymentInstruments[0].paymentCard?.cardType)
97103

98104
const submitForm = async (data) => {
105+
// Save the unique delivery addresses, excluding pickup shipments
99106
const saveShippingAddress = async (customerId) => {
100107
try {
101-
const shippingAddress = order.shipments[0].shippingAddress
102-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
103-
const {id, ...shippingAddressWithoutId} = shippingAddress
104-
const bodyShippingAddress = {
105-
addressId: nanoid(),
106-
...shippingAddressWithoutId
107-
}
108-
await createCustomerAddress.mutateAsync({
109-
body: bodyShippingAddress,
110-
parameters: {customerId: customerId}
108+
const storeLocatorEnabled =
109+
getConfig()?.app?.storeLocatorEnabled ?? STORE_LOCATOR_IS_ENABLED
110+
111+
const deliveryShipments = (order.shipments || []).filter((shipment) => {
112+
if (!shipment.shippingAddress) return false
113+
if (storeLocatorEnabled && isPickupShipment(shipment)) return false
114+
return true
111115
})
116+
117+
const uniqueAddresses = []
118+
deliveryShipments.forEach((shipment) => {
119+
const address = shipment.shippingAddress
120+
const isDuplicate = uniqueAddresses.some((existingAddr) =>
121+
areAddressesEqual(existingAddr, address)
122+
)
123+
if (!isDuplicate) {
124+
uniqueAddresses.push(address)
125+
}
126+
})
127+
128+
for (let i = 0; i < uniqueAddresses.length; i++) {
129+
const shippingAddress = uniqueAddresses[i]
130+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
131+
const {id, _type, ...shippingAddressWithoutId} = shippingAddress
132+
const bodyShippingAddress = {
133+
addressId: nanoid(),
134+
...shippingAddressWithoutId
135+
}
136+
await createCustomerAddress.mutateAsync({
137+
body: bodyShippingAddress,
138+
parameters: {customerId: customerId}
139+
})
140+
}
112141
} catch (error) {
113142
// Fail silently
114143
}

packages/template-retail-react-app/app/pages/checkout/confirmation.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,87 @@ describe('Account form', () => {
290290
expect(window.location.pathname).toBe('/uk/en-GB/account')
291291
})
292292
})
293+
294+
test('save delivery addresses but does not save pickup addresses when store locator is enabled', async () => {
295+
const savedAddresses = []
296+
const mockOrderWithPickup = {
297+
...mockOrder,
298+
shipments: [
299+
{
300+
...mockOrder.shipments[0],
301+
shipmentId: 'delivery-shipment',
302+
shippingAddress: {
303+
address1: '456 Delivery St',
304+
city: 'Vancouver',
305+
countryCode: 'CA',
306+
firstName: 'John',
307+
lastName: 'Doe',
308+
phone: '(604) 555-1234',
309+
postalCode: 'V6B 1A1',
310+
stateCode: 'BC'
311+
},
312+
shippingMethod: {
313+
id: 'standard-delivery',
314+
name: 'Standard Delivery'
315+
}
316+
},
317+
{
318+
shipmentId: 'pickup-shipment',
319+
shippingAddress: {
320+
address1: '789 Store Location Ave',
321+
city: 'Burnaby',
322+
countryCode: 'CA',
323+
firstName: 'Store',
324+
lastName: 'Pickup',
325+
phone: '(604) 555-5678',
326+
postalCode: 'V5H 2E2',
327+
stateCode: 'BC'
328+
},
329+
shippingMethod: {
330+
id: '005',
331+
name: 'Store Pickup',
332+
c_storePickupEnabled: true
333+
},
334+
c_fromStoreId: 'store-123'
335+
}
336+
]
337+
}
338+
339+
global.server.use(
340+
rest.get('*/orders/:orderId', (req, res, ctx) => {
341+
return res(ctx.delay(0), ctx.json(mockOrderWithPickup))
342+
}),
343+
rest.post('*/customers', (_, res, ctx) => {
344+
return res(ctx.status(200), ctx.json(mockCustomer))
345+
}),
346+
rest.post('*/customers/:customerId/addresses', (req, res, ctx) => {
347+
savedAddresses.push(req.body)
348+
return res(ctx.status(200))
349+
})
350+
)
351+
352+
const {user} = renderWithProviders(<MockedComponent />, {
353+
wrapperProps: {isGuest: true}
354+
})
355+
356+
const createAccountButton = await screen.findByRole('button', {name: /create account/i})
357+
const password = screen.getByLabelText('Password')
358+
await user.type(password, 'P4ssword!')
359+
await user.click(createAccountButton)
360+
361+
await waitFor(() => {
362+
expect(window.location.pathname).toBe('/uk/en-GB/account')
363+
})
364+
365+
// Verify that only one address was saved (the delivery address, not the pickup address)
366+
expect(savedAddresses).toHaveLength(1)
367+
expect(savedAddresses[0].address1).toBe('456 Delivery St')
368+
expect(savedAddresses[0].city).toBe('Vancouver')
369+
370+
// Verify the pickup address was NOT saved
371+
const hasPickupAddress = savedAddresses.some(
372+
(addr) => addr.address1 === '789 Store Location Ave'
373+
)
374+
expect(hasPickupAddress).toBe(false)
375+
})
293376
})

0 commit comments

Comments
 (0)