Support

Home Forums Event Espresso Premium Bug / issue on Duplicate event

Bug / issue on Duplicate event

Posted: June 10, 2019 at 12:50 am


hodyhong

June 10, 2019 at 12:50 am

I have found that when you duplicate an event, not all data will be copied over. Namely the followings:

  1. Mailchimp settings will be all gone. This is very annoying, as each time all the settings for Mailchimp will be need to reconfigure.
  2. Custom field data will not be duplicated. I use Advanced Custom Fields, and none these settings are copied. Nor a normal custom field data.

I have seen previous posts mention similar issues. But it seems these two issues were never fixed?

Any solution here.

Cheers

Hody


Tony

  • Support Staff

June 10, 2019 at 4:20 pm

Hi Hody,

Mailchimp settings will be all gone. This is very annoying, as each time all the settings for Mailchimp will be need to reconfigure.

This is something we can look into adding into the MailChimp add-on as it’s easier to wipe out the settings than it is to re-apply them if you have multiple lists/groups/question relationships.

For example, if you duplicate the event and the settings are copied over but you don’t want them, you can simply set the event to not subscribe to a list and update to wipe out the settings and step fresh. I imagine copying all of the options manually if you have multiple groups etc would get annoying.

I’ve created a ticket for us to investigate this further.

Custom field data will not be duplicated. I use Advanced Custom Fields, and none these settings are copied. Nor a normal custom field data.

This is something we will need to discuss further internally as if I recall correctly this was a design choice rather than a bug so this may not be something we add to core.

There’s a hook available that can be used to do whatever you want to do when an event is duplicated, that hooks is AHEE__Extend_Events_Admin_Page___duplicate_event__after which is passed the ‘new’ EE event object (the duplicate) and the ‘original’ event object.

So in the meantime, you could add your own function to copy whatever custom fields (or all of them) if you prefer.


hodyhong

June 11, 2019 at 12:42 am

Hi Tony, appreciate you looking at the Mailchimp issue. As there is quite a few event items we had to go through and we just want them to be opt into the same list with same group. But each time we had to redo them.

Thanks for the hook. Will definitely look into it.
Are there any function that I can use in combination of this hook to modify the event datetime? For example, I have got 6 datetimes of the original event, when I duplicate it I would like to shift the dates in these datetimes one day forward. Is it possible?

Thanks


Tony

  • Support Staff

June 11, 2019 at 4:16 am

Sure, you have the EE_Event object for both the new and original events so you can do whatever you need to do. The hook is fired after everything has been created.

If you’re asking for an example of how to do the above, I don’t have one as its not something we currently support, but if you work through the _duplicate_event() method used to duplicate the event you can see how that duplicates the datetimes (and tickets, which you’ll also need to extend) and can follow that.


hodyhong

June 12, 2019 at 12:30 am

Hi Tony, I attempted to get the following code snipet to work, but no luck.
Any hint? This is to add one day to all the datetime duplicated.


//Duplicate event acf and datetime update
add_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after', 'updateacfanddatetime');
function updateacfanddatetime($new_event, $orig_event) {

			$new_datetimes = $new_event->get_many_related('Datetime');
			foreach ($new_datetimes as $new_dtt) {
				
				$newdate = new DateTime(date("Y-m-d",strtotime($new_dtt->start_date()))) ;
					
				$newdateplusone = date_format($_newDate->add(new DateInterval('P1D')), 'F d, Y');
				
				$new_dtt->set_start_date($newdateplusone);
				$new_dtt->set_end_date($newdateplusone);
			}
			
}


hodyhong

June 12, 2019 at 2:56 am

The error messages was: Error message: Uncaught Error: Call to a member function get_many_related() on null …

It looks like the get_many_related() is not returning anything.


Tony

  • Support Staff

June 12, 2019 at 5:11 am

The problem is not that get_many_related() is not returning anything, it’s that $new_event is null at the time your running the code and you can not run a method on ‘nothing’, hence the error.

Your add_action call isn’t valid, your callback function expects 2 paramaters yet you are not telling the hook to pass 2 parameters, when testing your code I get a different fatal:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function updateacfanddatetime(), 1 passed in \wp-includes\class-wp-hook.php on line 288 and exactly 2 expected in \wp-content\plugins\ee-site-specific-plugin\ee-site-specific-plugin.php on line 849

You fix that by fixing your add_action call:

add_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after', 'updateacfanddatetime', 10, 2);

From there I get valid new_event and orig_event parameters with EE_Event objects, but your code throws another error, you’ll need to review and fix.


hodyhong

June 12, 2019 at 7:41 am

Hi Tony,

With your help, I have got rid of the errors. But now the problem was to set the $date variable for both $new_dtt->set_start_date() and $new_dtt->set_end_date(). I have tried a string with Dec. 25, 2025 or 12-25-2025, I wouldn’t work. I have also tried assigning it with a date object. It wouldn’t work either. Is there any hint on what the $date variable should be. I have looked into the EE_Datetime.class.php file.
Here is my current code:
add_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after', 'updateacfanddatetime', 10, 2);

function updateacfanddatetime($new_event, $orig_event) {
  $new_datetimes = $new_event->get_many_related('Datetime');
  foreach ($new_datetimes as $new_dtt) {
      $newdate = new DateTime(date("Y-m-d",strtotime($new_dtt->start_date()))) ;
      $newdateplusone = date_format($newdate->add(new DateInterval('P1D')), 'F d, Y');
      $new_dtt->set_start_date($newdateplusone);
      $new_dtt->set_end_date($newdateplusone);
 }
			
}


hodyhong

June 12, 2019 at 8:00 am

For the custom fields, the following code snippets will duplicate all the custom fields data over:

//Duplicate all custom fields.
 $orig_event_custom_fields = get_post_custom($orig_event->ID());
 foreach ( $orig_event_custom_fields as $field_key => $field_values ) {
    foreach ( $field_values as $key => $value ) update_post_meta( $new_event->ID(), $field_key, $value ); 
}


Tony

  • Support Staff

June 13, 2019 at 5:12 am

You’re missing a save() call to apply your changes, such as `$new_dtt->save();’

However, I did a little digging into this and as far as I can tell set_start_date() and set_end_date() don’t flag as changes in the models, so even with a save() call they won’t apply and save to the DB.

A quick fix is to add this after your code:

$new_dtt->set('DTT_EVT_start', $new_dtt->start_date_and_time());
$new_dtt->set('DTT_EVT_end', $new_dtt->end_date_and_time());

Then save, so it would look something like:

$new_dtt->set_start_date($newdateplusone);
$new_dtt->set_end_date($newdateplusone);
$new_dtt->set('DTT_EVT_start', $new_dtt->start_date_and_time());
$new_dtt->set('DTT_EVT_end', $new_dtt->end_date_and_time());
$new_dtt->save();

That should update the DateTime in the DB as set() does flag changes in the model.

With regards to the post meta values, thanks for sharing your code but something to note is that the above is fairly inefficient and may fail if you have a lot of custom fields.


hodyhong

June 14, 2019 at 3:02 am

Yes, this works! Thanks for the tips. So basically the time also need to be updated in order to flag it.

Regarding to the meta value, would there be a better way to do? I am not updating a lot, so it should be fine. But a better method would be beneficial for others to utilise for the future.


Tony

  • Support Staff

June 14, 2019 at 3:25 am

So basically the time also need to be updated in order to flag it.

No, it’s not the time, it’s a bug in the methods used.

In short set_start_date() and set_end_date() don’t set an internal flag to show that changes have been made to the datetime, so when save() is called it checks if that flag has changed and in this case it had not.

set() does change the internal flag and you could just use that directly if you pass it the full date and time to use rather than using set_start_date/set_end_date first.

I’ve created a ticket for our developers to investigate this further as I believe the above methods should be setting that flag but for now, the above is needed (and it won’t cause issues if this is ‘fixed’ in core).

Regarding to the meta value, would there be a better way to do? I am not updating a lot, so it should be fine. But a better method would be beneficial for others to utilise for the future.

Sure there is, you can do it in just 2 queries, take a look here:

https://rudrastyh.com/wordpress/duplicate-post.html

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

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3

Documentation for Event Espresso 4

Status: publish

Updated by  Tony 3 days, 13 hours ago ago

Topic Tags

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!