Extension Point: Validate Product Sku at Add-to-Cart
Basics
Parameter | Value |
---|---|
Extension Point Key | VALIDATE_PRODUCT_SKU_AT_ADD_TO_CART |
Extension Point Name | Validate Sku at Add-to-Cart |
Extension Interface | ProductSkuValidator |
Supports Multiple Extensions? | Yes |
Selector Type | Store |
Available Since | 1.0.0 |
Use Cases
Extensions implementing this Extension Point can enforce business rules that prevent items from being added to cart.
Methods
validate
The validate
method is invoked when an item is being added to a shopping cart. In Cortex, this happens when the add-to-cart operation is completed with a POST
operation. If one or more extensions returns StructuredErrorMessage
objects, then a 400 BAD REQUEST
response is returned, and the error message details are returned in the response body.
note
The Validate Shopping Item at Add-to-Cart Extension Point is invoked at the same time as this Extension Point.
Extension Sample
@Extension
@XPFAssignment(extensionPoint = XPFExtensionPointEnum.VALIDATE_PRODUCT_SKU_AT_ADD_TO_CART, priority = 100)
public class SampleProductSkuValidator extends XPFExtensionPointImpl implements ProductSkuValidator {
@Override
public Collection<StructuredErrorMessage> validate(final XPFProductSkuValidationContext context) {
List<StructuredErrorMessage> results = new ArrayList<>();
if (context.getProductSku().getAttributeValueByKey("block-purchase")
.map(attributeValue -> (boolean) attributeValue.getValue())
.orElse(false)) {
results.add(new StructuredErrorMessage("unpurchasable-product",
"Product has 'block-purchase' attribute set.", Collections.emptyMap()));
}
return results;
}
}