I know how to setup a Magento ecommerce store but I ran into this small issue where the quantity of products I can put in my cart exceeds the number of available stock in the database.

Example scenario; I have Product-X with a qty of 23 in my inventory. When a user views product-X, user has the option to choose buy qty by clicking on an increment(simple 'plus' sign) button. I would like this button to be disable when the qty counter shows 23(stock available in inventory). I can do this in basic PHP by matching with the database, how would you do this in Magento though?

Any help would be much appreciated.

  • When you add to cart, it should throw error saying stock exceeds. Does it? Jul 2, 2015 at 0:57

1 Answer 1


If I follow you well, you would like to make the user unable to set a quantity higher than the quantity stock in the input field of your product.

You can do that by putting your qty input with the type of "number", and adding the attribute "max", set to this quantity in stock. So if the browser of the client respect the HTML5 norms, a message will display, or the quantity will be stuck at this maximum value.

See this link for more information about input of type "number".

You could do it with something like that :

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
$maxQty = $stock->getQty();
<input type="number" name="..." id="..." max="<?php echo $maxQty; ?>">

Of course, replace the name and id with the base name and id of Magento where you are using this code. You should actually just change the type to number if it's not, and add the max attribute.

  • Wow, thats a pretty easy and straightforward solution I didnt think of, I will try this in a moment and get back with the results. Thanks for the answer
    – borislemke
    Jul 2, 2015 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.