Friday, September 24, 2021

Making an item "Sold Out" in Gravity Forms

Gravity Forms for Word Press  is pretty awesome, especially with integration with Stripe for selling things. The only problem I've run into is when I have a thing with a limited quantity and it sells out. There's a great snippet if you want to install some extra php. Best practices would be to use the snippet. I strongly encourage you to do so -- it's a better solution.

But if you want something faster to implement that can be installed by a developer who doesn't have the ability to upload/create php, you're going to need to use Javascript.

So adding a little javascript gets us there pretty easily:


<script type="application/javascript">
//get the existing quanity field you want to hide
var myfield=document.getElementById('ginput_quantity_12_7');
//create a span for the message
var soldoutSpan = document.createElement('span')
//make the message
soldoutSpan.innerHTML = '<b>SOLD OUT</b>';
//add the message AFTER the quantity field
myfield.parentNode.appendChild(soldoutSpan);
//hide the quantity field (you could delete it if you wanted to)
myfield.style.display="none";
//make your text red
soldoutSpan.style.color="red";
</script>

Of course you want to make sure you are validating your inputs for obvious reasons.