I hope you’re doing well. I need your help with a small issue. I’m currently working on a module to duplicate events for specific dates on my site. I’m retrieving the dates based on the format specified in the code files.
Here’s the function I’ve written:
function duplicate_event_function($EVT_ID)
{
if (! $EVT_ID) {
return;
}
// k we've got EVT_ID so let's use that to get the event we'll duplicate
$orig_event = EEM_Event::instance()->get_one_by_ID($EVT_ID);
if (! $orig_event instanceof EE_Event) {
throw new EE_Error(
sprintf(
esc_html__('An EE_Event object could not be retrieved for the given ID (%s)', 'event_espresso'),
$EVT_ID
)
);
}
// k now let's clone the $orig_event before getting relations
$new_event = clone $orig_event;
// original datetimes
$orig_datetimes = $orig_event->get_many_related('Datetime');
// other original relations
$orig_ven = $orig_event->get_many_related('Venue');
$new_event->set('EVT_ID', 0);
$new_name = $new_event->name();
$new_event->set('EVT_name', $new_name);
$new_event->set(
'EVT_slug',
wp_unique_post_slug(
sanitize_title($orig_event->name()),
0,
'publish',
EspressoPostType::EVENTS,
0
)
);
$new_event->set('status', 'draft');
// duplicate discussion settings
$new_event->set('comment_status', $orig_event->get('comment_status'));
$new_event->set('ping_status', $orig_event->get('ping_status'));
// save the new event
$new_event->save();
// venues
foreach ($orig_ven as $ven) {
$new_event->_add_relation_to($ven, 'Venue');
}
$new_event->save();
// now we need to get the question group relations and handle that
// first primary question groups
$orig_primary_qgs = $orig_event->get_many_related(
'Question_Group',
[['Event_Question_Group.EQG_primary' => true]]
);
if (! empty($orig_primary_qgs)) {
foreach ($orig_primary_qgs as $obj) {
if ($obj instanceof EE_Question_Group) {
$new_event->_add_relation_to($obj, 'Question_Group', ['EQG_primary' => true]);
}
}
}
// next additional attendee question groups
$orig_additional_qgs = $orig_event->get_many_related(
'Question_Group',
[['Event_Question_Group.EQG_additional' => true]]
);
if (! empty($orig_additional_qgs)) {
foreach ($orig_additional_qgs as $obj) {
if ($obj instanceof EE_Question_Group) {
$new_event->_add_relation_to($obj, 'Question_Group', ['EQG_additional' => true]);
}
}
}
$new_event->save();
// k now that we have the new event saved we can loop through the datetimes and start adding relations.
$cloned_tickets = [];
foreach ($orig_datetimes as $orig_dtt) {
if (! $orig_dtt instanceof EE_Datetime) {
continue;
}
$new_dtt = clone $orig_dtt;
$orig_tickets = $orig_dtt->tickets();
$new_dtt->set('DTT_ID', 0);
$new_dtt->set('DTT_sold', 0);
$new_dtt->set_reserved(0);
$new_dtt->save();
$new_event->_add_relation_to($new_dtt, 'Datetime');
$new_event->save(); //new date for events are saved
foreach ((array) $orig_tickets as $orig_ticket) {
if (! $orig_ticket instanceof EE_Ticket) {
continue;
}
// is this ticket archived? If it is then let's skip
if ($orig_ticket->get('TKT_deleted')) {
continue;
}
// does this original ticket already exist in the clone_tickets cache?
// If so we'll just use the new ticket from it.
if (isset($cloned_tickets[ $orig_ticket->ID() ])) {
$new_ticket = $cloned_tickets[ $orig_ticket->ID() ];
} else {
$new_ticket = clone $orig_ticket;
//this date and time is not getting saved for the ticket.
$new_ticket->set_end_date('12-12-2025');
$new_ticket->set_end_time('9 AM');
// get relations on the $orig_ticket that we need to set up.
$orig_prices = $orig_ticket->prices();
$new_ticket->set('TKT_ID', 0);
$new_ticket->set('TKT_sold', 0);
$new_ticket->set('TKT_reserved', 0);
// make sure new ticket has ID.
$new_ticket->save();
// price relations on new ticket need to be setup.
foreach ($orig_prices as $orig_price) {
// don't clone default prices, just add a relation
if ($orig_price->is_default()) {
$new_ticket->_add_relation_to($orig_price, 'Price');
$new_ticket->save();
continue;
}
$new_price = clone $orig_price;
$new_price->set('PRC_ID', 0);
$new_price->save();
$new_ticket->_add_relation_to($new_price, 'Price');
}
$new_ticket->save();
$cloned_tickets[ $orig_ticket->ID() ] = $new_ticket;
}
// k now we can add the new ticket as a relation to the new datetime
// and make sure it's added to our cached $cloned_tickets array
// for use with later datetimes that have the same ticket.
$new_dtt->_add_relation_to($new_ticket, 'Ticket');
}
$new_dtt->save();
}
// clone taxonomy information
$taxonomies_to_clone_with = apply_filters(
'FHEE__Extend_Events_Admin_Page___duplicate_event__taxonomies_to_clone',
['espresso_event_categories', 'espresso_event_type', 'post_tag']
);
// get terms for original event (notice)
$orig_terms = wp_get_object_terms($orig_event->ID(), $taxonomies_to_clone_with);
// loop through terms and add them to new event.
foreach ($orig_terms as $term) {
wp_set_object_terms($new_event->ID(), $term->term_id, $term->taxonomy, true);
}
// do_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after', $new_event, $orig_event);
// now let's redirect to the edit page for this duplicated event if we have a new event id.
if ($new_event->ID()) {
$redirect_args = [
'post' => $new_event->ID(),
'action' => 'edit',
];
EE_Error::add_success(
esc_html__(
'Event successfully duplicated. Please review the details below and make any necessary edits',
'event_espresso'
)
);
} else {
$redirect_args = [
'action' => 'default',
];
EE_Error::add_error(
esc_html__('Not able to duplicate event. Something went wrong.', 'event_espresso'),
__FILE__,
__FUNCTION__,
__LINE__
);
}
return $new_event->ID();
}
The issue I’m facing seems to be with these two lines:
$new_ticket->set_end_date('12-12-2025'); $new_ticket->set_end_time('9 AM');
The date and time for the ticket aren’t saved properly. Please look and let me know where I might be going wrong and how to fix it.
Is named that way to not cause confusion between the ticket end_datetime and assigning datetimes to tickets, but technically, it would be better if it could have been named set_end_datetime() because it does NOT just set date, it sets both date and time.
Which means $new_ticket->set_end_date('12-12-2025'); isn’t a valid PHP datetime value and will return false which then defaults to today within PHP.
So you need $new_ticket->set_end_date('12-12-2025 9:00AM');
And you can skip $new_ticket->set_end_time('9 AM');, that method is for specifically setting the time, but you don’t need it in this case as you would just set it with the date above.
Grabs a ticket.
Outputs current end datetime.
Sets using set_end_date('12-12-2025') (outputs today’s date)
Sets using set_end_date('12-12-2025 9:00AM') (Outputs expected datetime)
Hi Tony,
I have checked with this function, but this line is also output today’s date.
$new_ticket = clone $orig_ticket;
//This date and time are not being saved.
$new_ticket->set_end_date('12-12-2025 9:00AM');
I just forgot to tell you I had used this format as well but facing the same issue it was returning today’s date.
$new_ticket->set_end_date(’12/12/2025 9:00AM);`
Hello Tony,
I need to follow up on this ticket.
Thanks!
Viewing 12 reply threads
The support post ‘Issue with Setting Ticket Date and Time in Duplicate Event Function’ is closed to new replies.
Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.
Support forum for Event Espresso 3 and Event Espresso 4.