Skip Navigation

[Resolved] Send notification to user based on custom date field

This support ticket is created 4 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 16 replies, has 2 voices.

Last updated by jayesonE 4 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1875185

When a member registers or updates their profile to our site and if they have a contract they have to fill in the "End of contact date" field.
Based on this date we want to send them an email notification two weeks prior to the date to remind then to let us know if they are renewing their contact or cancelling they account.

If they are renewing then we want them to update the End of contract date field or let us know to cancel the account.
Can the cancel account be automated using the notification email.
The most important part is sending out the notification.

I found this snippet for a post and am guessing could be modified to fit for a user

add_filter( 'cred_notification_event_type', 'my_custom_event_type', 99, 4);

function my_custom_event_type($notification_type, $form_id){
if( $form_id == '123' )
// Return a slug for your custom notification event type. This can be anything, but must be unique.
return 'custom_notification_event_type';
}

// Create a function that checks whether a specific condition is met, when a custom event type is used.
add_filter( 'cred_custom_notification_event_type_condition', 'my_custom_event', 99, 4);

function my_custom_event( $bool, $notification, $form_id, $post_id ){
if( $form_id == '123' ){
$date_field = get_post_meta( $post_id, 'wpcf-my-date-field', true );
$current_date = date('Y-m-d');
if( strtotime( $current_date ) == $date_field )
return true;
}
}

Thanks

#1875221

Hello,

The cred_notification_event_type will create a custom event after user submit the form, see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_event_type

In your case, it needs custom codes, for example:
1) create a WordPress CRON job:
https://developer.wordpress.org/plugins/cron/

2) In above CRON job, get all users whose "End of contract date field" user field value is two weeks prior to the date:
https://developer.wordpress.org/reference/classes/wp_user_query/

3) Then send the email notification:
https://developer.wordpress.org/reference/functions/wp_mail/

#1876367

Thank you for the quick response. I'll give it a try.

#1876503

OK, please update here if you still need assistance for it, thanks

#1876573

Not sure what is correct here. I can see the contract_cron_job nothing much else
Any advice would be appreciated.

add_action( 'contract_cron_hook', 'contract_cron_exec' ); {

if ( ! wp_next_scheduled( 'contract_cron_hook' ) ) {

wp_schedule_event( time(), 'twicedaily', 'contract_cron_hook' );

$before_date = date("Ymd", strtotime("+2 week"));
$user_query = new WP_User_Query( array(
'meta_key' => 'wpcf-contract-end-date',
'meta_query' => array (
0 =>
array (
'key' => 'wpcf-contract-end-date',
'value' => '$before_date',
'compare' => '<',
'type' => 'DATE',
),

), ) );

/// cred notification event type
add_filter( 'cred_notification_event_type', 'contractend_event_type', 99, 4);
function contractend_event_type($notification_type, $form_id){

if( $form_id == '3843' )//or 3857
add_filter( 'cron_schedules', 'contactend_add_cron_interval' );

wp_mail( 'wordpress@myemailaddress.dd', 'The Notification subject', 'The Notification message'); //minium for testing

// Return a slug for your custom notification event type. This can be anything, but must be unique.
return 'contractend_notification';
}
// end cred notification event type

}
}

#1876597

How do you setup the custom field "contract-end-date", is it a custom date field created with Types plugin?
If it is, Types custom date field stores value in timestamp format, you don't need to transfer the value to date format, for example, replace this line from:
$before_date = date("Ymd", strtotime("+2 week"));
To:
$before_date = date("U", strtotime("+2 week"));

#1876617

Hi Luo
Thank You, Yes I made the custom field with Types plugin. I will add the new code and test.

#1877663

Please update here if you still need assistance for it, thanks

#1877715

I still cannot get this to work especially getting the toolset custom field wpcf-contract-end-date to display.
I've tried to run the wp_user_query on its own but it does not display the proper user profiles that have the wpcf-contract-end-date set
This is my latest version. I've tried to put things into the order you suggested originally but not having any luck. Do I have the code in the proper order?

// Add a new notification event type and then check whether a specific condition is met. If it is, return the slug that will be used for your custom notification event type.
add_filter( 'cred_notification_event_type', 'my_custom_event_type', 99, 4);

function my_custom_event_type($notification_type, $form_id){
if( $form_id == '3857' ) //or 3843

//query to see if contract date is two weeks from today

// Create cron job
wp_schedule_event( time(), 'twicedaily', 'contractend_cron_hook' );
if ( ! wp_next_scheduled( 'contactend_cron_hook' ) ) {

// WP_User_Query arguments
$before_date = date("U", strtotime("+2 week"));
$args = array(
'role' => 'member',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpcf-contact-end-date',
'value' => $before_date,
'compare' => '<',
'type' => 'DATE',
),
),

);

// The User Query
$user_contract_query = new WP_User_Query( $args );

// The User Loop
if ( ! empty( $user_contract_query->results ) ) {
foreach ( $user_contract_query->results as $user ) {

wp_mail( 'wordpress@myaddress.dd', 'The Notification subject', 'The Notification message'); // do something

}
} else {
echo(' Nothing to do'); // no users found
}
}

// Return a slug for your custom notification event type. This can be anything, but must be unique.
return 'custom_notification_event_type';
}

// End of new notification event type

#1877807

the above code is full of errors, I will fix and resend.

#1877817

This should be better but still not working.
// Add a new notification event type and then check whether a specific condition is met. If it is, return the slug that will be used for your custom notification event type.
add_filter( 'cred_notification_event_type', 'my_custom_event_type', 99, 4);

function my_custom_event_type($notification_type, $form_id){
if( $form_id == '3857' ) //or 3843

//query to see if contract date is two weeks from today

// Create cron job
wp_schedule_event( time(), 'twicedaily', 'contractend_cron_hook' );
if ( ! wp_next_scheduled( 'contactend_cron_hook' ) ) {

// WP_User_Query arguments
$before_date = date("U", strtotime("+2 week"));
$args = array(
'role' => 'member',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpcf-contact-end-date',
'value' => $before_date,
'compare' => '<',
'type' => 'DATE',
),
),

);

// The User Query
$user_contract_query = new WP_User_Query( $args );

// The User Loop
if ( ! empty( $user_contract_query->results ) ) {
foreach ( $user_contract_query->results as $user ) {

wp_mail( 'wordpress@myaddress.dd', 'The Notification subject', 'The Notification message'); // do something

}
} else {
echo(' Nothing to do'); // no users found
}
}

// Return a slug for your custom notification event type. This can be anything, but must be unique.
return 'custom_notification_event_type';
}

// End of new notification event type

#1877855

Since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and form URL, where I can edit your custom PHP codes, I need to test and debug it in a live website, thanks

#1878491

Ok, that would be great. The server is ip locked so please send me your ip address so I can give you access

#1878897

My IP address: 129.226.11.13

#1878953

Thanks
Can you please re-send the ftp and WordPress form so I can send those details to you?