how to add, add to cart button on custom product page

Topic summary

A user is building a custom product page and needs help implementing a fully functional “Add to Cart” button. The requirements include:

Core functionality needed:

  • Variant selection capability
  • Quantity selector
  • Auto-disable when product is out of stock
  • Quantity limits based on available inventory

Solution provided:
Another user shared code snippets including:

  • Liquid markup for the product form with variant and quantity selectors
  • JavaScript to prevent quantity input from exceeding available stock
  • Dynamic max attribute on quantity input tied to inventory levels

Current issue (unresolved):
The quantity limiter only reads the first variant’s stock level and doesn’t update when switching between variants. The user has requested a solution to make the stock limit dynamically update based on the currently selected variant.

Summarized with AI on October 27. AI used: claude-sonnet-4-5-20250929.

how to add, add to cart button with full functionality like variant selection and qty selection and also it should disabled when product is stocked out , can anybody help me with this?

Hi @satyam_gupta ,

You can add a full “Add to Cart” button with variant and quantity selection by following these steps:

In your product template (e.g., main-product.liquid or product-form.liquid), include:


hey what there is error, what if there is only one qty available of the product then it should be only increase qty to the stocked qty, if there is one stock then it should not be increased to 2 …

Hi,

To prevent the quantity input from exceeding the available stock, especially when there’s only 1 item in stock, you need to:

  1. Set the max attribute dynamically on the quantity input.

  2. Add JS to disable the + button or restrict input if needed.

  3. Update input with max attribute


  1. Add this JS to prevent typing above stock

but when i am selecting a other variant so it is catching only first variant qty , please assist and provide solution for that?