import * as ecomValidations from 'interfaces-ecommerce-v1-validations-provider';
const MIN_AMOUNT = 2500; // Set the minimum amount required
export const getValidationViolations = async (options, context) => {
const { lineItems } = options.validationInfo;
const violations = [];
console.log('Line Items:', lineItems); // Log the line items to see their structure
// Calculate total cart value by summing the price of each line item
const totalAmount = lineItems.reduce((sum, item) => {
return sum + (item.price * item.quantity);
}, 0);
console.log('Total Amount:', totalAmount); // Log the total amount
// Check if the total amount is below the minimum required amount
if (totalAmount < MIN_AMOUNT) {
const amountTooLowViolation = {
severity: "ERROR",
target: {
other: {
name: "MIN_AMOUNT_VIOLATION" // Unique identifier for the violation
}
},
description: `Your cart total must be at least ₱${MIN_AMOUNT}.` // Message for the user
};
violations.push(amountTooLowViolation); // Push violation if total is below minimum
}
console.log('Violations:', violations); // Log the violations array
return { violations }; // Return any validation violations found
}; is this the correct way ? of having a min amount validation?
Hi Chi,
Thanks for sharing. Looks like your going in the right direction. How did testing go?
Best,
Eitan