Skip to content

Instantly share code, notes, and snippets.

@heggemsnes
Created September 9, 2020 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heggemsnes/ecdf423fe55ba2c9e2eefa7b32ac8411 to your computer and use it in GitHub Desktop.
Save heggemsnes/ecdf423fe55ba2c9e2eefa7b32ac8411 to your computer and use it in GitHub Desktop.
Create user for each ticket holder on Woocommerce Box Office.
<?php
// Needs to run when creating order
// Also needs to run when editing a ticket
function create_users_from_ticket($order_id)
{
if (!wc_get_order($order_id)) {
return;
}
// Get Tickets from order ID
$tickets = get_tickets_by_order($order_id);
if (function_exists('wc_box_office_get_ticket_email_contacts')) {
foreach ($tickets as $ticket) {
$ticket_id = $ticket->ID;
add_user_to_ticket_from_email($ticket_id);
}
}
}
add_action('woocommerce_thankyou', 'create_users_from_ticket', 10, 1);
function add_user_to_ticket_from_email($ticket_id)
{
// Find emails in ticket. Only works with 1 email at the moment.
$user_email = wc_box_office_get_ticket_email_contacts($ticket_id)[0];
// If has ticket_holder_user meta, remove this.
// This is to remove previous owner if user changes email on ticket
if (get_post_meta($ticket_id, 'ticket_holder_user')) {
delete_post_meta($ticket_id, 'ticket_holder_user');
}
$user_id = email_exists($user_email);
// If user doesn't exist we create a new one
if (!$user_id) {
// Password is auto generated and sent to user
$user_id = wc_create_new_customer($user_email, $user_email);
}
// Add meta connecting ticket to user
update_post_meta($ticket_id, 'ticket_holder_user', $user_id);
}
add_action('woocommerce_box_office_after_edit_ticket_form', 'add_user_to_ticket_from_email', 10, 1);
// Helper function to get tickets of user
function get_user_holder_tickets($user_id)
{
$args = array(
'post_type' => 'event_ticket',
'post_status' => array( 'publish', 'pending' ),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'ticket_holder_user',
'value' => $user_id,
),
),
);
return get_posts($args);
}
// Helper function for getting tickets for order
function get_tickets_by_order($order_id)
{
$args = array(
'post_type' => 'event_ticket',
'post_status' => array( 'publish', 'pending' ),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_order',
'value' => $order_id,
),
),
);
return get_posts($args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment