Skip to content

Commit 0b41ed1

Browse files
committed
fix: ignore shipping_country_code_lock for order with digital products only
1 parent d53fbf5 commit 0b41ed1

8 files changed

Lines changed: 75 additions & 38 deletions

File tree

@typings/checkout.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface CheckoutSettings {
1313
orderNumber: number
1414
validCheckout: true
1515
isGuest: boolean
16+
isShipmentRequired: boolean
1617
endpoint: string
1718
domain: string
1819
slug: string

components/composite/CheckoutContainer/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const CheckoutContainer = ({ settings, children }: Props): JSX.Element => {
2525
<AppProvider
2626
orderId={settings.orderId}
2727
isGuest={settings.isGuest}
28+
isShipmentRequired={settings.isShipmentRequired}
2829
accessToken={settings.accessToken}
2930
slug={settings.slug}
3031
domain={settings.domain}

components/composite/StepCustomer/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ export const StepCustomer: React.FC<Props> = () => {
108108
forceShipping,
109109
disableToggle,
110110
}: ShippingToggleProps) => {
111-
if (forceShipping) {
112-
setShipToDifferentAddress(true)
111+
if (isShipmentRequired) {
112+
if (forceShipping) {
113+
setShipToDifferentAddress(true)
114+
}
115+
setDisabledShipToDifferentAddress(disableToggle)
113116
}
114-
setDisabledShipToDifferentAddress(disableToggle)
115117
}
116118

117119
const handleSave = async (params: { success: boolean; order?: Order }) => {

components/data/AppProvider/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { ActionType, reducer } from "components/data/AppProvider/reducer"
1111
import {
1212
calculateSettings,
1313
checkAndSetDefaultAddressForOrder,
14-
checkIfShipmentRequired,
1514
fetchOrder,
1615
FetchOrderByIdResponse,
1716
} from "components/data/AppProvider/utils"
@@ -88,6 +87,7 @@ interface AppProviderProps {
8887
slug: string
8988
orderId: string
9089
isGuest: boolean
90+
isShipmentRequired: boolean
9191
accessToken: string
9292
children?: ChildrenType
9393
}
@@ -96,6 +96,7 @@ export const AppProvider: React.FC<AppProviderProps> = ({
9696
children,
9797
orderId,
9898
isGuest,
99+
isShipmentRequired,
99100
accessToken,
100101
slug,
101102
domain,
@@ -121,7 +122,6 @@ export const AppProvider: React.FC<AppProviderProps> = ({
121122
}
122123
dispatch({ type: ActionType.START_LOADING })
123124
const order = await getOrderFromRef()
124-
const isShipmentRequired = await checkIfShipmentRequired(cl, orderId)
125125

126126
const addressInfos = await checkAndSetDefaultAddressForOrder({
127127
cl,
@@ -161,8 +161,6 @@ export const AppProvider: React.FC<AppProviderProps> = ({
161161
dispatch({ type: ActionType.START_LOADING })
162162
const currentOrder = order ?? (await getOrderFromRef())
163163

164-
const isShipmentRequired = await checkIfShipmentRequired(cl, orderId)
165-
166164
const others = calculateSettings(
167165
currentOrder,
168166
isShipmentRequired,

components/data/AppProvider/utils.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from "@commercelayer/sdk"
1717

1818
import { AppStateData } from "components/data/AppProvider"
19-
import { LINE_ITEMS_SHIPPABLE } from "components/utils/constants"
2019

2120
export type LineItemType =
2221
| "gift_cards"
@@ -283,32 +282,6 @@ export const fetchOrder = async (cl: CommerceLayerClient, orderId: string) => {
283282
})
284283
}
285284

286-
export async function checkIfShipmentRequired(
287-
cl: CommerceLayerClient,
288-
orderId: string
289-
): Promise<boolean> {
290-
const lineItems = (
291-
await cl.orders.retrieve(orderId, {
292-
fields: {
293-
line_items: ["item_type", "item"],
294-
},
295-
include: ["line_items", "line_items.item"],
296-
})
297-
).line_items?.filter(
298-
(line_item) =>
299-
LINE_ITEMS_SHIPPABLE.includes(line_item.item_type as TypeAccepted) &&
300-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
301-
// @ts-ignore
302-
!line_item.item?.do_not_ship
303-
)
304-
305-
if (lineItems?.length === undefined) {
306-
return false
307-
}
308-
// riguardare
309-
return lineItems.length > 0
310-
}
311-
312285
export function isPaymentRequired(order: Order) {
313286
return !(order.total_amount_with_taxes_float === 0)
314287
}

specs/e2e/guest-addresses.spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { faker } from "@faker-js/faker"
22

33
import { test, expect } from "../fixtures/tokenizedPage"
4-
import { euAddress, euAddress2 } from "../utils/addresses"
4+
import { euAddress, euAddress2, usAddress } from "../utils/addresses"
55

66
const customerEmail = faker.internet.email().toLocaleLowerCase()
77

@@ -263,6 +263,45 @@ test.describe("with customer email and shipping country code lock", () => {
263263
})
264264
})
265265

266+
test.describe("with digital product and shipping country code lock", () => {
267+
test.use({
268+
defaultParams: {
269+
order: "digital",
270+
orderAttributes: {
271+
customer_email: customerEmail,
272+
shipping_country_code_lock: "IT",
273+
},
274+
},
275+
})
276+
277+
test.skip("Checkout different country code address", async ({
278+
checkoutPage,
279+
}) => {
280+
await checkoutPage.checkOrderSummary("Order Summary")
281+
282+
const email = await checkoutPage.getCustomerMail()
283+
284+
await expect(email).toHaveValue(customerEmail)
285+
286+
await checkoutPage.checkStep("Customer", "open")
287+
288+
await checkoutPage.setBillingAddress()
289+
290+
await checkoutPage.selectCountry("billing_address", "FR")
291+
await checkoutPage.page.fill(
292+
"[data-testid=input_billing_address_state_code]",
293+
"PA"
294+
)
295+
296+
await checkoutPage.isVisibleShipToDifferentAddress(false)
297+
298+
await checkoutPage.checkButton({ type: "Customer", status: "enabled" })
299+
300+
await checkoutPage.save("Customer")
301+
await checkoutPage.checkStep("Customer", "close")
302+
})
303+
})
304+
266305
test.describe("with shipping country code lock and different address", () => {
267306
test.use({
268307
defaultParams: {

specs/fixtures/CheckoutPage.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,17 @@ export class CheckoutPage {
226226
)
227227
}
228228

229+
async isVisibleShipToDifferentAddress(visible: boolean) {
230+
const element = this.page.locator(
231+
"[data-testid=button-ship-to-different-address]"
232+
)
233+
if (visible) {
234+
await expect(element).toBeVisible()
235+
} else {
236+
await expect(element).toBeHidden()
237+
}
238+
}
239+
229240
async useCustomerCard() {
230241
const element = this.page.getByTestId("customer-card")
231242
await expect(element).toBeVisible({ timeout: 10000 })

utils/getSettings.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import retry from "async-retry"
88
import jwt_decode from "jwt-decode"
99

1010
import { TypeAccepted } from "components/data/AppProvider/utils"
11-
import { LINE_ITEMS_SHOPPABLE } from "components/utils/constants"
11+
import {
12+
LINE_ITEMS_SHIPPABLE,
13+
LINE_ITEMS_SHOPPABLE,
14+
} from "components/utils/constants"
1215

1316
const RETRIES = 2
1417

@@ -107,9 +110,9 @@ async function getOrder(
107110
"privacy_url",
108111
"line_items",
109112
],
110-
line_items: ["item_type"],
113+
line_items: ["item_type", "item"],
111114
},
112-
include: ["line_items"],
115+
include: ["line_items", "line_items.item"],
113116
})
114117
)
115118
}
@@ -205,6 +208,14 @@ export const getSettings = async ({
205208
return invalidateCheckout()
206209
}
207210

211+
const isShipmentRequired = (order.line_items || []).some(
212+
(line_item) =>
213+
LINE_ITEMS_SHIPPABLE.includes(line_item.item_type as TypeAccepted) &&
214+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
215+
// @ts-ignore
216+
!line_item.item?.do_not_ship
217+
)
218+
208219
if (order.status === "draft" || order.status === "pending") {
209220
// Logic to refresh the order is documented here: https://github.com/commercelayer/mfe-checkout/issues/356
210221
if (!paymentReturn && (!order.autorefresh || (!isGuest && order.guest))) {
@@ -231,6 +242,7 @@ export const getSettings = async ({
231242
slug,
232243
orderNumber: order.number || 0,
233244
orderId: order.id,
245+
isShipmentRequired,
234246
validCheckout: true,
235247
logoUrl: organization.logo_url,
236248
companyName: organization.name || "Test company",

0 commit comments

Comments
 (0)