Tickets are handled similar to events, and can query via the Craft Element Criteria Model (opens new window). To get all tickets for a specified event you need to provide the eventId
.
{% for ticket in craft.events.tickets.eventId(event.id) %}
Because tickets can have a Available From
and Available To
parameter you need to be aware of this. Events provides a simple helper function to get all available tickets. The eventId
is also required again.
{% for ticket in craft.events.availableTickets(event.id) %}
Purchased tickets are stored in a extra table. You can get these via craft.events.purchasedTickets()
. You can give this function up to 2 parameters:
attributes
: list of attribute values (indexed by attribute names) that the active records should match.options
: query condition or criteria.Events can have a maximum capacity, which you can check against:
{% set purchasedEventTickets = craft.events.purchasedTickets({ eventId: event.id }) | length %}
{% if purchasedEventTickets < event.capacity %}
// Your ticket list here...
{% endif %}
Tickets can have a maximum quantity, which you can also check against:
{% set purchasedTickets = craft.events.purchasedTickets({ ticketId: ticket.id }) | length %}
{% if purchasedTickets < ticket.quantity %}
//...
{% endif %}
A simple ticket selection list can look like following template:
{# check for event limit #}
{% set purchasedEventTickets = craft.events.purchasedTickets({ eventId: event.id }) | length %}
{% if purchasedEventTickets < event.capacity %}
<form method="POST">
<input type="hidden" name="action" value="commerce/cart/updateCart">
<input type="hidden" name="redirect" value="shop/cart">
<input type="hidden" name="qty" value="1">
{{ getCsrfInput() }}
<select name="purchasableId" class="purchasableId">
{%- for ticket in craft.events.availableTickets(event.id) -%}
{# check for ticket limits #}
{% set purchasedTickets = craft.events.purchasedTickets({ ticketId: ticket.id }) | length %}
{% if purchasedTickets < ticket.quantity %}
<option value="{{ ticket.purchasableId }}">
{{ ticket }} - {{ ticket.price|commerceCurrency(cart.currency) }}
</option>
{%- endif -%}
{%- endfor -%}
</select>
<button type="submit">{{ "Add to cart"|t }}</button>
</form>
{% else %}
<strong>Sold out</strong>
{% endif %}
Events can be provided as a multi ticket selection list. To use it you need to call the custom front-end controller events/cart/add
.
A complete integration can look like following template:
<form method="POST">
{# custom frontend controller #}
<input type="hidden" name="action" value="events/cart/add">
<input type="hidden" name="redirect" value="shop/cart">
{{ getCsrfInput() }}
<table width="100%" border="0" cellpadding="0" cellspacing="0">
{% for ticket in craft.events.availableTickets(event.id) %}
<tr>
<td>{{ ticket }}</td>
<td>{{ ticket.price | commerceCurrency(cart.currency) }}</td>
<td align="right" nowrap="nowrap">
<input type="hidden" name="event" value="{{ event.id }}">
{# check for ticket limits #}
{% set purchasedTickets = craft.events.purchasedTickets({ ticketId: ticket.id }) | length %}
{% set availableTickets = ticket.quantity - purchasedTickets %}
{% if availableTickets > 0 %}
<div class="field dropdown">
<div class="input">
<select name="item[{{ ticket.id }}][qty]" class="ticket_table_select">
{% set maxDropdown = (availableTickets > 10) ? 10 : availableTickets %}
{% for i in 0..maxDropdown %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
</select>
</div>
</div>
{% else %}
<strong>Sold out</strong>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="{{ "Add to cart"|t }}" class="button"/>
</form>
Once a user has purchased a ticket, its important to actually provide the ticket SKU for them to use at the event. For example, on your order summary template (shop/customer/order.html
for example), you could have the following code looping through line items for the order.
{% if craft.events.isTicket(item) %}
{% for purchasedTicket in item.purchasable.getPurchasedTicketsForLineItem(item) %}
Ticket-SKU: {{ purchasedTicket }}<br />
{% endfor %}
{% endif %}
Because multiple tickets can be purchased for one line item, its important to loop through potentially multiple unique ticket SKU's as above.
If you're looking for something a little prettier than simply showing the ticket SKU in your order confirmation page or email, you may want to look at generating a PDF Template.