Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafiahmedd/4110c145d52be0e0ca9f7457e278e400 to your computer and use it in GitHub Desktop.
Save rafiahmedd/4110c145d52be0e0ca9f7457e278e400 to your computer and use it in GitHub Desktop.
Add users to a list or tag who subscribe last week from today in CRM and trigger an email automation
/**
* Please use this code inside the theme function.php file
* or it can throw error on your site
*/
/**
* this method will register event to WordPress init
*/
add_action( 'init', 'SendEmailToLastWeekContacts');
function SendEmailToLastWeekContacts(){
if( !wp_next_scheduled( 'notify_last_week_contacts' ) ) {
// schedule an event
//Available Event Types (hourly, twicedaily, daily, weekly, fifteendays, monthly)
wp_schedule_event( time(), 'hourly', 'notify_last_week_contacts' );
}
}
add_action( 'notify_last_week_contacts', 'addToListForEmail' );
/**
* this method will call when cron executes
*/
function addToListForEmail()
{
$data = wpFluent()->table('fc_subscribers')->where('status', 'subscribed')->get(); // Getting all subscribers
$totalData = count($data); // Counting subscribers number
$contactApi = FluentCrmApi('contacts');
$daysToValidate = 7; // Please make sure you are adding the number of days only e.g. ( 60 = 2months, 7 = 1week, 2 = 2 days)
$list = [4]; // ID number of the list. Replace 4 with your list id number
//$tag = [4]; // ID number of the tag. Replace 4 with your tag id number
for ($i = 0; $i<$totalData; $i++) {
if ($data[$i]->status == 'subscribed') {
$to = new DateTime(current_time('Y-m-d H:i:s')); // Today
$from = new DateTime($data[$i]->created_at); // Created Date Time
$diff = $to->diff($from)->format("%a"); // Calculating the day diff and converting it in days
if($diff >= $daysToValidate){
$contact = $contactApi->getContact($data[$i]->email);
$contact->attachLists($list); // Attaching to a list for the automation
//$contact->attachTags($tag); // Attaching tag for the automation
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment