Let’s dive into the code first.
View this code snippet on GitHub.How do I install this snippet?
Easy peasy. Just copy and paste the code above into your theme's functions.php file.
Do I need to modify this snippet to work with my form?
You don’t have to modify the snippet itself. Just the parameters with which you initialize it.
View this code snippet on GitHub.Parameters
$form_id
the ID of the form you are working with.$field_id
the ID of your product or quantity field. If using a Single Product field, you’ll need to specify the quantity input ID which will always be{field ID}.3
(ie if the field ID is12
, the quantity input ID will be12.3
). If using a separate Quantity field, simply specify the field ID.$limit
the number of this item you have available.$limit_message
the message which should be displayed to users when the product limit has been reached. You can get fancy with this and add HTML as well. Be sure to escape any double quotes with a backslash. Here’s a more advanced example: View this code snippet on GitHub.Better Inventory: Limit Message
$validation_message
the validation message which should be displayed on the field if the limit has not been reached, but the user’s requested quantity would exceed the product limit.Better Inventory: Limit Validation Message
$approved_payments_only
true/false
indicate whether all submissions for this field should be counted against the limit or only submissions with an approved payment. You’ll want to be careful here when using PayPal Standard due to the delay between the form submission and the payment being approved. It could lead to users exceeding the limit.$hide_form
by default this snippet will only hide the field when the limit has been reached. If you would rather hide the entire form, set this totrue
.