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
11 changes: 7 additions & 4 deletions packages/template-retail-react-app/app/hooks/use-einstein.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export class EinsteinAPI {
namespace: 'useEinstein.einsteinFetch',
additionalProperties: {error: error}
})
return {}
}

if (!response?.ok) {
Expand Down Expand Up @@ -342,15 +343,16 @@ export class EinsteinAPI {
/**
* Tells the Einstein engine when a user starts the checkout process.
**/
async sendBeginCheckout(basket, args) {
async sendBeginCheckout(basket, args = {}) {
const endpoint = `/activities/${this.siteId}/beginCheckout`
const method = 'POST'
const products = basket.productItems.map((item) => this._constructEinsteinItem(item))
const subTotal = basket.productSubTotal
const body = {
products: products,
amount: subTotal,
...args
checkoutType: 'traditional', // Default to traditional for backward compatibility
...args // Allow override (e.g., checkoutType: 'one-click')
}

return this.einsteinFetch(endpoint, method, body)
Expand All @@ -360,14 +362,15 @@ export class EinsteinAPI {
* Tells the Einstein engine when a user reaches the given step during checkout.
* https://developer.salesforce.com/docs/commerce/einstein-api/references#einstein-recommendations:Summary
**/
async sendCheckoutStep(stepName, stepNumber, basket, args) {
async sendCheckoutStep(stepName, stepNumber, basket, args = {}) {
const endpoint = `/activities/${this.siteId}/checkoutStep`
const method = 'POST'
const body = {
stepName,
stepNumber,
basketId: basket.basketId,
...args
checkoutType: 'traditional', // Default to traditional for backward compatibility
...args // Allow override (e.g., checkoutType: 'one-click')
}

return this.einsteinFetch(endpoint, method, body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,18 @@ export const CheckoutProvider = ({children}) => {
// Run this once when checkout begins
useEffect(() => {
if (basket?.productItems) {
einstein.sendBeginCheckout(basket)
einstein.sendBeginCheckout(basket, {
checkoutType: 'one-click'
})
}
}, [])

// Run this every time checkout steps change
useEffect(() => {
if (step != undefined) {
einstein.sendCheckoutStep(getCheckoutStepName(step), step, basket)
einstein.sendCheckoutStep(getCheckoutStepName(step), step, basket, {
checkoutType: 'one-click'
})
}
}, [step])

Expand Down
Loading