During the checkout process of SimpleCart you have the ability to change the amount of a product in the cart overview. As of right now this is not really safe because anyone (with a little console knowledge) can manipulate the maximum amount of a product so it exceeds the current stock. That’s not what I want.
The checkout is handled with FormIt, so I assume I need to make a Custom Validator.
Here’s my approach so far:
// html form part for the product quantity
<input type="number" name="quantity[[[+product.key]]]" min="0" max="[[+product.product_stock]]" value="[[+product.totals.quantity]]" />
// FormIt
[[!FormIt?
&store=`1`
&hooks=`validate,spam,redirect`
&submitVar=`checkout`
&redirectTo=`[[*id:scFirstChild]]`
&customValidators=`checkStock`
&validate=`quantity[[[+product.key]]]:checkStock`
]]
// checkStock Snippet
$stock = $modx->resource->getTVValue('product_stock');
$value = (int)$value;
$success = $value >= $stock;
if (!$success) {
$validator->addError($key,'Not enough in Stock!');
}
return $success;
- Is this the right approach or would that also be somehow possible with FormIt’s
maxValue
validator? - If I have the right approach, I’m quite sure these things are causing trouble:
- the field name
quantity[[[+product.key]]]
- the definition of the
$stock
variable in the Snippet
- the field name
Thanks for any help!