<p>I am trying to figure out a way to set a maximum quantity at the product level instead of on the individual variants.</p>
<p>The specific use case is selling tickets to events, where my client needs to be able to limit the maximum amount of tickets they can sell for a given event. However, each event can have multiple variants (full ticket, student ticket, etc.).</p>
<p>My current solution uses a custom number field on the product, and then I simply query all completed orders and count how many times the specific variant has been sold. But this seems very hacky and has a lot of potential edge cases.</p>
<p>I am somewhat new to Commerce so I might be missing something obvious, but I haven't been able to find anything in the documentation.</p>
<p>Any suggestions?</p>

CC BY-SA 4.0
2022-08-04T16:05:39.533Z
Just to clarify, are you talking about the maximum that can be bought in a single transaction, or are you talking about the total stock? (i.e., the maximum that can be bought across the whole store?). Commerce has two different fields for managing those things, but as you have noticed, if a product is configured to have variants, both those fields are held at the variant level instead of the product level.
James Smith
CC BY-SA 4.0
2022-08-04T17:16:16.130Z
@JamesSmith The latter. I want to limit the total stock across all variants for a given product. For example a product could be a ticket to an event, and I want to set a hard limit of 500 regardless of the combination of variants sold.
Peter U
CC BY-SA 4.0
2022-08-05T10:13:19.633Z
I'm fairly sure Commerce doesn't provide anything out of the box for your use case unfortunately. It'd make a good feature request though, or a custom plugin.
James Smith
CC BY-SA 4.0
2022-08-08T12:18:40.713Z
@JamesSmith Thank you for taking the time. I solved this by creating a custom module that prevents lineitems from being added or sold if it exceeds the maximum allowed.
Peter U
CC BY-SA 4.0
2024-07-29T08:03:14.797Z
any chance you could share the module code base with me @PeterU? either here or privately. attempting something similar but not quite there yet
James Greig
CC BY-SA 4.0
2024-08-01T12:51:13.027Z
@JamesGreig Here you go. I have just added two product fields (eventNumSold and eventMaxCapacity). All the hardcoded warning texts are in Danish, but the code should be somewhat self-explanatory. Hope it helps.

https://gist.github.com/peterurban/30930ca7bcadb5dd361ca96f88c490d5
Peter U
<craft4><commerce4>
Set maximum quantity at the product level
# Set maximum quantity at the product level

(Asked by: username_0 on 2022-08-04)

I am trying to figure out a way to set a maximum quantity at the product level instead of on the individual variants.

The specific use case is selling tickets to events, where my client needs to be able to limit the maximum amount of tickets they can sell for a given event. However, each event can have multiple variants (full ticket, student ticket, etc.).

My current solution uses a custom number field on the product, and then I simply query all completed orders and count how many times the specific variant has been sold. But this seems very hacky and has a lot of potential edge cases.

I am somewhat new to Commerce so I might be missing something obvious, but I haven't been able to find anything in the documentation.

Any suggestions?

**Tags:** craft4, commerce4

> **Comments:**
> **username_1**: Just to clarify, are you talking about the maximum that can be bought in a single transaction, or are you talking about the total stock? (i.e., the maximum that can be bought across the whole store?). Commerce has two different fields for managing those things, but as you have noticed, if a product is configured to have variants, both those fields are held at the variant level instead of the product level.  
> **username_0**: @JamesSmith The latter. I want to limit the total stock across all variants for a given product. For example a product could be a ticket to an event, and I want to set a hard limit of 500 regardless of the combination of variants sold.  
> **username_1**: I'm fairly sure Commerce doesn't provide anything out of the box for your use case unfortunately. It'd make a good feature request though, or a custom plugin.  
> **username_0**: @JamesSmith Thank you for taking the time. I solved this by creating a custom module that prevents lineitems from being added or sold if it exceeds the maximum allowed.  
> **username_2**: any chance you could share the module code base with me @PeterU? either here or privately. attempting something similar but not quite there yet  
> **username_0**: @JamesGreig Here you go. I have just added two product fields (eventNumSold and eventMaxCapacity). All the hardcoded warning texts are in Danish, but the code should be somewhat self-explanatory. Hope it helps.  https://gist.github.com/peterurban/30930ca7bcadb5dd361ca96f88c490d5  

---

## 0 answers

