Jump to content

Prevent quantity go above stock quantity


Carpe_diem

Recommended Posts

Hi guys,

I have just discovered a problem in my Prestashop 1.7.8.7 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 () {
		updateButtonState($(this));
	});

	$(window).on("resize", function () {
		updateButtonState($(".js-cart-line-product-quantity"));
         location.reload();
	});

	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

			$touchspinContainer.find(".custom-span").remove();

			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);
				$messageContainer.text(""); 
			}
		});
	}

	updateButtonState($(".js-cart-line-product-quantity"));

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...