[FormIt + SimpleCart] validate product stock on checkout

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&#91;[[+product.key]]&#93;" min="0" max="[[+product.product_stock]]" value="[[+product.totals.quantity]]" />
// FormIt

// 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;
  1. Is this the right approach or would that also be somehow possible with FormIt’s maxValue validator?
  2. If I have the right approach, I’m quite sure these things are causing trouble:
    • the field name quantity&#91;[[+product.key]]&#93;
    • the definition of the $stock variable in the Snippet

Thanks for any help!

Any idea, how to tackle this or some suggestions where I could start from?

Does SimpleCart not already kick back the order if the order exceeds available stock?

Unfortunately it doesn’t seem to by default and I haven’t found any settings for this. I also didn’t find similar questions on the web though, which makes me wondering if I’m missing something obvious here…

The way I prohibited adding more products to the cart as are actually available is by setting the max value of the number input. But as this is also very easily “hacked”, I need to verify the stock finally during the checkout process.

<input type="number" name="quantity" min="1" max="[[+product_stock]]" value="1" />

That’s interesting, I can’t seem to find where SimpleCart validates the number of items in the cart are within the available stock. Which suggests… it doesn’t do that and nobody noticed in the last 6 years :grimacing: (or my search-fu is failing me right now)

I’ve logged an issue for this and will report back if that’s added. In the meantime, may I suggest a more modern cart solution?

1 Like

@markh That really seems hard to believe but maybe it is that way :sweat_smile:

For some reason I can’t click on your link?

Whoops, fixed my link. Thanks @lkfranklin.