Following up on this thread which is marked as resolved:
https://toolset.com/forums/topic/notification-setup-for-dynamic-expiration-date/
I am using a CRON event to send a notification email to the author of the post when the post expiration time is less than 10 days.
// Send 10 days expiry notification email
add_action( 'ten_day_poster_expire_notification_hook', 'ten_day_poster_expire_notification_function' );
function ten_day_poster_expire_notification_function() {
$args = array(
'post_status' => 'publish',
'posts_per_page' => -1
);
$published_posts = get_posts( $args );
$subject = 'Your poster subscription is running out';
$site_link = get_site_url();
$site_name = get_bloginfo();
foreach( $published_posts as $post ) : setup_postdata($post);
$post_title = get_the_title($post->ID);
$post_links = get_permalink($post->ID);
$expiry_time = get_post_meta( $post->ID, '_cred_post_expiration_time', true);
$expiry_time_formatted = date("F j, Y, g:i a", $expiry_time);
$author_email = get_the_author_meta( 'user_email', $post->post_author );
$body = '<p>Your subscription for the <a href="' . $post_links . '">' . $post_title .'</a> poster ends on <strong>' . $expiry_time_formatted . '</strong>. ' . 'Click <a href="' . $post_links . '">here</a> to renew it.</p><p>Kind Regards</p><p><strong>The <a href="' . $site_link .'">' . $site_name .'</a> team.</strong></p> ' ;
$headers = array('Content-Type: text/html; charset=UTF-8');
$email_sent_status = get_post_meta( $post->ID, 'wpcf-ten-days-expiration-email-status', true);
if ( (time() > $expiry_time - 60*60*240) && ($email_sent_status != "email sent") ) {
wp_mail( $author_email, $subject, $body, $headers );
update_post_meta( $post->ID, 'wpcf-ten-days-expiration-email-status', "email sent");
}
endforeach;
wp_reset_postdata();
}
I use this code and not the CRED expiry functionality because my expiration date '_cred_post_expiration_time' is depended on a custom field that the user will select to extend his/her subscription. Tha also means that I have completely deactivated the "Enable Automatic Expiration of Post options for CRED Post Forms" from CRED settings.
I have a custom field 'wpcf-ten-days-expiration-email-status' set to "email not sent". Whenever I fire up the CRON event(using the "WP Crontrol" plugin) the value of that custom field is set to "email sent" and the notification email reaches teh author. The problem I have is that around 5 minutes after the first email, an identical email also reached the author(with the same timestamp as the first email) and I don't understand why that is happening since the 'wpcf-ten-days-expiration-email-status' custom field has been set to "email sent". After the second email is sent, behaviour is as expected, no other email is sent after the CRON event is fired.
Thank you for contacting Toolset support, I'd be delighted to assist!
There may be an issue regarding Toolset's caching which could be causing CRED notifications to not send. As I don't want to run you through troubleshooting until we diagnose the caching issue, please await an update from me by tomorrow evening on the Caching issue - we should have a determination of if this is causing notifications not to fire.
I don't think that has anything to do with it. I create my own notification and not depend on the CRED ones. Also it's sending the notifications fine, the problem is that it sends it twice and not once as per code.
Hello George!
As I am not a PHP developer myself and can only assist with stock Toolset functionality, I would kindly ask you to get in touch with one of our consultants: https://toolset.com/consultant/
I do realize that Lou may of provided custom code examples and there is is a grey area regarding this as Toolset is a complex suite of plugins.
If you'd like I can reassign this ticket back to Lou to see if he could efficiently resume assisting you in this matter but please know regardless of supporter, custom code is a best effort basis and technically isn't in the scope of support - especially considering the use of internal Toolset APIs.
I appreciate your understanding!
Hi Ross, yes, could you please give Lou a shout, he might be able to see something in the code I am not seeing.
Thanks!
No worries, I have reassigned the ticket to Lou!
I am sorry but I got an automated reply for this threat if I still wanted any help. I still want help and I thought Rosss had reassigned the ticket to Lou.
Dear George,
Could you describe the question with more details?
Is the action hook "ten_day_poster_expire_notification_hook" from another plugin/theme?
Since it is a custom PHP codes problem, please duplicate same problem in a test site, fill the login details and ftp access in below private detail box, also point out the problem page URL and CRED form, and where I can edit your PHP codes.