How to limit product quantity in Shopify

I wanted to limit the quantity of an item that can be ordered in Shopify and I came up with this idea.

The logic:

Check the cart for a particular item name and quantity, if the quantity is greater than 1, run a script to change it to 1

The code:

open up theme editor and edit the cart.liquid file

Under:

<form action="/cart" method="post" id="cartform">

<ul class="item-list divider">

{% for item in cart.items %}

Add the following line:

{% if item.product.handle == '[YOUR ITEM HANDLE]' and item.quantity > 1%}

<script>

 window.location.href='/cart/change?line=1&quantity=1';

</script>

{% endif %}

P.S: Replace the [YOUR ITEM HANDLE] above

I used the item.product.handle to avoid white spaces

To find the item.product.handle, add the following code under the 'endif' code above

{{ item.product.handle }}

You can also change the number of quantity from 1 to whatever you desire.

You can remove the item.product.handle check and apply the logic to all products.

Maybe someone has a better option but this worked well for me.