Search.../

getValidationViolations( )

Retrieves any validation violations in a site visitor's cart or checkout.

Description

The getValidationViolations function validates a site visitor's cart or checkout and returns any validation violations. Site visitors can see the validation violations in their cart and checkout pages. If there aren't any validation violations, the endpoint returns an object containing an empty list.

The function is automatically called by Wix eCommerce when certain actions are performed on a cart or checkout. For example, when an item is added to a cart, or when a coupon is added to a checkout.

Note: By default, the Validations SPI only validates a site visitor's checkout. If you want to also validate a site visitor's cart, set the validateInCart parameter to true in the custom extension's config file.

Where to find getValidationViolations()

When you add the Validations custom extension, a folder is automatically added to your site. Use the <my-extension-name>.js file in the folder to write the code to determine which validation violations to retrieve.

For more information on customizing your payment settings, see Tutorial: Validations Custom Extension.

Syntax

function getValidationViolations(options: Options): Promise<GetValidationViolationsResponse>

getValidationViolations Parameters

NAME
TYPE
DESCRIPTION
options
Options

Validation options.

Returns

Fulfilled - Validation violations.

Return Type:

Promise<GetValidationViolationsResponse>
NAME
TYPE
DESCRIPTION
violations
Array<Violation>

List of validation violations.

Was this helpful?

Get Validation Violations

This example validates a cart and checkout. The quantity of gold rings is limited, and the site visitor isn't allowed to purchase more than 5.

Copy Code
1const goldRingItemId = "37e5db61-d37d-442d-1bf5-ae755f82020b";
2
3export const getValidationViolations = async (options, context) => {
4 const lineItems = options.validationInfo.lineItems;
5 let violations = [];
6
7 if (hasExcessiveGoldRingQuantity(lineItems)) {
8 const source = options.sourceInfo.source;
9 const severity = getSourceSeverity(source);
10
11 const target = {
12 lineItem: {
13 name: 'LINE_ITEM_DEFAULT',
14 _id: findGoldRingLineItemId(lineItems)
15 }
16 };
17
18 const description = "You can't purchase more than 5 gold rings.";
19
20 const violation = createViolation(severity, target, description);
21 violations.push(violation);
22 }
23
24 return { violations };
25};
26
27function getSourceSeverity(source) {
28 if (source === 'CART') {
29 return 'WARNING';
30 } else {
31 return 'ERROR';
32 }
33}
34
35function hasExcessiveGoldRingQuantity(lineItems) {
36 for (const lineItem of lineItems) {
37 if (lineItem.catalogReference.catalogItemId === goldRingItemId && lineItem.quantity > 5) {
38 return true;
39 }
40 }
41 return false;
42}
43
44function findGoldRingLineItemId(lineItems) {
45 for (const lineItem of lineItems) {
46 if (lineItem.catalogReference.catalogItemId === goldRingItemId) {
47 return lineItem._id;
48 }
49 }
50 return undefined;
51}
52
53function createViolation(severity, target, description) {
54 return { severity, target, description };
55}