Skip to main content

useBuyerJourneyIntercept
hook

Installs a function for intercepting and preventing progress on checkout.

To block checkout progress, you must set the block_progress capability in your extension's configuration.

If you do, then you're expected to inform the buyer why navigation was blocked, either by passing validation errors to the checkout UI or rendering the errors in your extension.

required

Was this section helpful?

Anchor to interceptorrequestblockInterceptorRequestBlock

Block the buyer from proceeding further in the checkout

"block"
required

Indicates that the interceptor will block the buyer's journey from continuing.

string
required

The reason for blocking the interceptor request. This value isn't presented to the buyer, so it doesn't need to be localized. The value is used only for Shopify’s own internal debugging and metrics.

[]

Used to pass errors to the checkout UI, outside your extension's UI boundaries.

(result: ) => void | Promise<void>

This callback is called when all interceptors finish. We recommend setting errors or reasons for blocking at this stage, so that all the errors in the UI show up at once.

Was this section helpful?

Anchor to interceptorrequestallowInterceptorRequestAllow

Allow the buyer to proceed further in the checkout

"allow"
required

Indicates that the interceptor will allow the buyer's journey to continue.

(result: ) => void | Promise<void>

This callback is called when all interceptors finish. We recommend setting errors or reasons for blocking at this stage, so that all the errors in the UI show up at once.

Was this section helpful?

Block progress and show error for a checkout UI field

React

import {
reactExtension,
useBuyerJourneyIntercept,
useShippingAddress,
} from '@shopify/ui-extensions-react/checkout';

export default reactExtension(
'purchase.checkout.delivery-address.render-before',
() => <Extension />,
);

function Extension() {
const address = useShippingAddress();

useBuyerJourneyIntercept(
({canBlockProgress}) => {
return canBlockProgress &&
address?.countryCode &&
address.countryCode !== 'CA'
? {
behavior: 'block',
reason: 'Invalid shipping country',
errors: [
{
message:
'Sorry, we can only ship to Canada',
// Show an error underneath the country code field
target:
'$.cart.deliveryGroups[0].deliveryAddress.countryCode',
},
{
// In addition, show an error at the page level
message:
'Please use a different address.',
},
],
}
: {
behavior: 'allow',
};
},
);

return null;
}

In addition to targeting checkout UI fields, you can also pass errors to the page level or render the error in your extension.

Anchor to example-block-progress-and-show-error-at-page-levelBlock progress and show error at page level

Intercept and prevent a buyer's progress through checkout while displaying an error message at the page level. See the validation tutorial for more examples and best practices.

Anchor to example-block-progress-and-show-error-in-your-extensionBlock progress and show error in your extension

Intercept and prevent a buyer's progress through checkout while displaying an error message in your extension. See the validation tutorial for more examples and best practices.

Was this section helpful?

Block progress and show error at page level

React

import {
reactExtension,
useBuyerJourneyIntercept,
useShippingAddress,
} from '@shopify/ui-extensions-react/checkout';

export default reactExtension(
'purchase.checkout.delivery-address.render-before',
() => <Extension />,
);

function Extension() {
const address = useShippingAddress();

useBuyerJourneyIntercept(
({canBlockProgress}) => {
return canBlockProgress &&
address?.countryCode &&
address.countryCode !== 'CA'
? {
behavior: 'block',
reason: 'Invalid shipping country',
errors: [
{
// An error without a `target` property is shown at page level
message:
'Sorry, we can only ship to Canada',
},
],
}
: {
behavior: 'allow',
};
},
);

return null;
}