Vendure Validate Cart Plugin
npm install @pinelab/vendure-plugin-validate-cartVendure Validate Cart Plugin
Section titled “Vendure Validate Cart Plugin”A very lightweight plugin to have custom cart validation rules before proceeding to checkout. For example to check current stock, or check for limited products. You can implement your own validation rules by implementing the CartValidatorStrategy interface.
Getting started
Section titled “Getting started”Add the plugin to your config:
import { ValidateCartPlugin, DefaultStockValidationStrategy,} from '@pinelab/vendure-plugin-validate-cart';
// In your plugins array, in vendure-config.tsplugins: [ ValidateCartPlugin.init({ // This is just the sample strategy that checks if the cart has enough stock for the items in the cart. validationStrategy: new DefaultStockValidationStrategy(), }),];This will add a validateActiveOrder mutation to the shop API.
Storefront usage
Section titled “Storefront usage”Depending on your validation strategy, you probably want to call the validateActiveOrder mutation in the cart page, and again on the payment page:
mutation ValidateActiveOrderMutation { validateActiveOrder { errors { message errorCode relatedOrderLineIds } order { id lines { productVariant { product { id } } } } }}If any validation errors are returned, you can display the error to the user with the corresponding order lines. You could even automatically remove the items from the cart based on the relatedOrderLineIds. The order field returns the active order after validation, including any modifications made by a custom validation strategy.
This action is a mutation rather than a query, because your custom validation logic might need to modify the order, for example to ‘lock’ an order after validation until payment is completed.
Custom validation rules
Section titled “Custom validation rules”You can implement your own validation rules by implementing the CartValidatorStrategy interface. For example, to check if active promotions are still valid for the items in the cart.
import { ValidateCartStrategy, ActiveOrderValidationError,} from '@pinelab/vendure-plugin-validate-cart';
export class PromotionValidator implements ValidateCartStrategy { // Tell the plugin to pre-load the promotions for the order loadOrderRelations: RelationPaths<Order> = ['promotions'];
// Validate the order based on your custom logic async validate(ctx: RequestContext, activeOrder: Order, injector: Injector) { const errors: ActiveOrderValidationError[] = []; for (const promotion of activeOrder.promotions) { if (!isPromotionValid(promotion)) { errors.push({ message: `Promotion ${promotion.name} is not valid`, errorCode: `INVALID_PROMOTION`, }); } } return errors; }}