Retrieves any validation violations in a site visitor's cart or checkout.
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 service plugin 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 service plugin's config file.
getValidationViolations()
When you add the Validations service plugin,
a folder is automatically added to your site. Use the .js
file in the folder to write the code to determine which validation violations to retrieve.
For more information on customizing your checkout validations plugin, see Tutorial: Validations Service Plugin.
function getValidationViolations(
options: Options,
context: Context,
): Promise<GetValidationViolationsResponse>;
Validation options.
Metadata about the request.
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.
const goldRingItemId = "37e5db61-d37d-442d-1bf5-ae755f82020b";
export const getValidationViolations = async (options, context) => {
const lineItems = options.validationInfo.lineItems;
let violations = [];
if (hasExcessiveGoldRingQuantity(lineItems)) {
const source = options.sourceInfo.source;
const severity = getSourceSeverity(source);
const target = {
lineItem: {
name: "LINE_ITEM_DEFAULT",
_id: findGoldRingLineItemId(lineItems),
},
};
const description = "You can't purchase more than 5 gold rings.";
const violation = createViolation(severity, target, description);
violations.push(violation);
}
return { violations };
};
function getSourceSeverity(source) {
if (source === "CART") {
return "WARNING";
} else {
return "ERROR";
}
}
function hasExcessiveGoldRingQuantity(lineItems) {
for (const lineItem of lineItems) {
if (
lineItem.catalogReference.catalogItemId === goldRingItemId &&
lineItem.quantity > 5
) {
return true;
}
}
return false;
}
function findGoldRingLineItemId(lineItems) {
for (const lineItem of lineItems) {
if (lineItem.catalogReference.catalogItemId === goldRingItemId) {
return lineItem._id;
}
}
return undefined;
}
function createViolation(severity, target, description) {
return { severity, target, description };
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.