Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ethanclevenger91/98f1101ca0da176de5cbb3a08bf3c05a to your computer and use it in GitHub Desktop.
Save ethanclevenger91/98f1101ca0da176de5cbb3a08bf3c05a to your computer and use it in GitHub Desktop.
Programmatically add a Tribe Events Calendar event with tickets and attendee meta
<?php
$event_id = tribe_create_event( [
'post_title' => 'My Event',
// see https://docs.theeventscalendar.com/reference/functions/tribe_create_event/ for full arguments
] );
$provider = \Tribe__Tickets__Tickets::get_event_ticket_provider();
$ticket_id = $provider::get_instance()->ticket_add( $event_id, [
'ticket_name' => 'Test ticket ' . uniqid(),
'ticket_provider' => $provider,
'ticket_price' => '100',
'tribe-ticket' => [
'mode' => 'global',
'event_capacity' => '100',
'capacity' => ''
],
'ticket_description' => 'Wine and cheese night',
'ticket_show_description' => '1',
'ticket_start_date' => '',
'ticket_start_time' => '',
'ticket_end_date' => '',
'ticket_end_time' => '',
'ticket_sku' => uniqid(),
'ticket_id' => '',
] );
// Optionally, you can also add attendee meta when using TEC PRO
// You can use an existing template by passing the ID to an existing ticket-meta-fieldset post
$ticket_fields = get_post_meta( 10734, \Tribe__Tickets_Plus__Meta__Fieldset::META_KEY, true );
// Or you can define your own programatically
$ticket_fields = [
[
"type" => "text",
"required" => "on",
"label" => "First Name",
"slug" => "first-name",
"extra" => [],
],
[
"type" => "email",
"required" => "on",
"label" => "Email",
"slug" => "email",
"extra" => [],
],
[
"type" => "select",
"required" => "on",
"label" => "State",
"slug" => "state",
"extra" => [
"options" => [
"Alabama",
"Alaska",
"Arizona",
// ...
"Wisconsin",
"Wyoming",
],
],
],
[
"type" => "telephone",
"required" => "on",
"label" => "Mobile Phone",
"slug" => "mobile-phone",
"extra" => [],
],
];
update_post_meta( $ticket_id, \Tribe__Tickets_Plus__Meta::META_KEY, $ticket_fields );
update_post_meta( $ticket_id, \Tribe__Tickets_Plus__Meta::ENABLE_META_KEY, 'yes' );
@tnntwister
Copy link

thanks, you saved my day !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment