Limit Order Line Quantity
Guide to limit order line quantity in Umbraco Commerce.
ProductAddValidationHandler
public class ProductAddValidationHandler : ValidationEventHandlerBase<ValidateOrderProductAdd>
{
private readonly IProductService _productService;
public ProductAddValidationHandler(IProductService productService)
{
_productService = productService;
}
public override async Task ValidateAsync(ValidateOrderProductAdd evt)
{
var order = evt.Order;
var productReference = evt.ProductReference;
var stock = await _productService.GetProductStockAsync(productReference);
var totalQuantities = order?.OrderLines.Where(x => x.ProductReference == productReference).Sum(x => x.Quantity) ?? 0;
if (stock.HasValue && totalQuantities >= stock.Value)
evt.Fail($"Only {stock} quantities can be purchased for {productReference}.");
}
}
OrderLineQuantityValidationHandler
Register event handlers
Last updated
Was this helpful?