Jump to content

Prevent quantity go above stock quantity


Recommended Posts

Hi guys,

I have just discovered a problem in my Prestashop on cart page. The problem I am facing is that customers can add quantites above the available stock. Let say Product A has a stock of 5 and the customer can add 10. 
Prestashop shows a notification alert that a product is not available the quantity that they have entered.
It gets even more messy and not understandable when there are mulitple products in the cart with the quantities above the stock and it does not say which product need to be decreased. 

I tried to fix it with jQuery code that I have added below:

	$(document).on("input change", ".js-cart-line-product-quantity", function () {

	$(window).on("resize", function () {

	function updateButtonState($quantityInput) {
		$quantityInput.each(function () {
			var maxStockQty = parseFloat($(this).attr("max"));
			var minStockQty = parseFloat($(this).attr("min"));
			var currentQty = parseFloat($(this).val());

			var $relatedButton = $(this).closest(".js-cart-line").find(".bootstrap-touchspin-up");
			var $touchspinContainer = $relatedButton.closest(".bootstrap-touchspin"); // Assuming .bootstrap-touchspin is the container element

			var $messageContainer = $(this).next(".message-container"); // Use next to select the element after the input


			if (currentQty >= maxStockQty) {
				$relatedButton.prop("disabled", true);
				$touchspinContainer.append("<span class='custom-span'>Quantity exceeds available stock.</span>");
			} else if (currentQty <= minStockQty) {
				$relatedButton.prop("disabled", true);
				$touchspinContainer.append("<span class='custom-span'>Quantity below minimum allowed.</span>");
			} else {
				$relatedButton.prop("disabled", false);


it kinda works. It breaks when I quickly change the quantity to a lower number and then back to a higher and it goes above the max number. The AJAX that checks the quantity and calculates the final price  and that breaks my code and it doesn't work. 

How do I solve this problem so customers can't set quantity above the stock available for selected product.


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...