Support

Home Forums Event Espresso Premium Issue with Setting Ticket Date and Time in Duplicate Event Function

Issue with Setting Ticket Date and Time in Duplicate Event Function

Posted: December 11, 2024 at 12:50 am

Viewing 0 reply threads


Mark

December 11, 2024 at 12:50 am

Hi Tony,

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;

//old event datetime
$old_DTT_EVT_start = $orig_dtt->get('DTT_EVT_start');
$old_DTT_EVT_end = $orig_dtt->get('DTT_EVT_end');

$day = 'Monday';

//new date for events are saving
$newStartDate = getNextDayOccurrenceWithTime($old_DTT_EVT_start, $day);

$new_dtt->set_start_date($newStartDate['date']);
$new_dtt->set_start_time($newStartDate['time']);

$newEndDate = getNextDayOccurrenceWithTime($old_DTT_EVT_end, $day);
$new_dtt->set_end_date($newEndDate['date']);
$new_dtt->set_end_time($newEndDate['time']);

$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);
}

// duplicate page_template setting
$page_template = get_post_meta($orig_event->ID(), '_wp_page_template', true);
if ($page_template) {
update_post_meta($new_event->ID(), '_wp_page_template', $page_template);
}

// 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.

I would appreciate your assistance.

Best regards,
Mark

Viewing 0 reply threads

You must be logged in to reply to this support post. Sign In or Register for an Account

Event Espresso