Skip to content

Instantly share code, notes, and snippets.

@aliaramli
Last active October 3, 2020 05:43
Show Gist options
  • Save aliaramli/7cf35892a779d8c6e72a2ac032966307 to your computer and use it in GitHub Desktop.
Save aliaramli/7cf35892a779d8c6e72a2ac032966307 to your computer and use it in GitHub Desktop.
// WP_CRON
// register monthly interval
add_filter('cron_schedules','my_cron_definer');
function my_cron_definer($schedules){
$schedules['monthly'] = array('interval'=> 2592000, 'display'=> __('Once Every 30 Days') );
return $schedules;
}
// JOB : SEND EMAIL TO USERS WHO IS NOT ONLINE FOR MORE THAN 1 MONTH. NOTIFY THAT THEIR PRODUCT WILL BE REMOVE
// IF THEY STILL NOT ONLINE FOR 3 MONTHS.
if ( ! wp_next_scheduled( 'send_email_to_non_active_sellers_hook' ) ) {
// 10 PM (seller usually active at night)
wp_schedule_event( strtotime('22:00:00'), 'monthly', 'send_email_to_non_active_sellers_hook' );
}
add_action( 'send_email_to_non_active_sellers_hook', 'send_email_to_non_active_sellers_function' );
/**
* Send an alert by email.
*/
function send_email_to_non_active_sellers_function() {
//select user that is offline ensure that users are sellers
//select the user_id and wc_last_active
global $wpdb;
$offline_sellers = $wpdb->get_results("select user_id, meta_value as last_active from wp_usermeta where meta_key= 'wc_last_active' and user_id in (select user_id from wp_usermeta where meta_key in ('fal_user_online_status') and meta_value=0 and user_id in (select user_id from wp_usermeta where meta_key = 'wp_capabilities' and meta_value like '%wcfm_vendor%'));");
//sellers not active more than 3 months
$none_active_sellers_more_than_3_months = array();
//sellers not active more than 1 months
$none_active_sellers_more_than_1_months = array();
foreach($offline_sellers as $seller) {
//get the users that are not active more than 3 month
if ($seller->last_active < date('U', strtotime('-3 month', time()))) {
array_push($none_active_sellers_more_than_3_months, $seller->user_id);
} else if ($seller->last_active > date('U', strtotime('-3 month', time())) and $seller->last_active < date('U', strtotime('-1 month', time()))) {
//get the users that are not active more than 1 month but not exceeding 3 months
array_push($none_active_sellers_more_than_1_months,$seller->user_id);
}
}
define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));
//send email to the sellers that are not active more than 1 month
foreach($none_active_sellers_more_than_1_months as $none_active_seller_more_than_1_month){
//get email
$user_info = get_userdata($none_active_seller_more_than_1_month);
$user_name = $user_info->display_name;
$user_email = $user_info->user_email;
//SEND EMAIL
$to = $user_email;
$subject = 'We miss you '.$user_name.'. You have not online for sometime..';
$heading = 'Dear '.$user_name.', you have not online for sometime..';
$message = '<p>Please get online to sell your product,
or we will automatically remove your products after you have not online for 3 months.</p>';
$headers = array('Content-Type: text/html; charset=UTF-8');
send_email_woocommerce_style($to, $subject, $heading, $message);
sleep(2);
}
//delete sellers posts that are not active for more than 3 months
foreach($none_active_sellers_more_than_3_months as $none_active_seller_more_than_3_month){
$args = array (
'numberposts' => -1,
'post_type' => 'product',
'author' => $none_active_seller_more_than_3_month
);
// get all posts by this user: posts, pages, attachments, etc..
$seller_posts = get_posts($args);
if (empty($seller_posts)) continue;
// delete all the user posts
foreach ($seller_posts as $seller_post) {
wp_delete_post($seller_post->ID, true);
sleep(2);
}
}
//SEND EMAIL
$to = 'example@gmail.com';
$subject = 'Cron has started and deleted unactive seller posts';
$heading = 'Checkout the latest numbers of posts';
$message = '<p>Checkout the latest numbers of posts at https://foodah.my</p>';
$headers = array('Content-Type: text/html; charset=UTF-8');
send_email_woocommerce_style($to, $subject, $heading, $message);
}
// @email - Email address of the reciever
// @subject - Subject of the email
// @heading - Heading to place inside of the woocommerce template
// @message - Body content (can be HTML)
function send_email_woocommerce_style($email, $subject, $heading, $message) {
// Get woocommerce mailer from instance
$mailer = WC()->mailer();
// Wrap message using woocommerce html email template
$wrapped_message = $mailer->wrap_message($heading, $message);
// Create new WC_Email instance
$wc_email = new WC_Email;
// Style the wrapped message with woocommerce inline styles
$html_message = $wc_email->style_inline($wrapped_message);
// Send the email using wordpress mail function
wp_mail( $email, $subject, $html_message, HTML_EMAIL_HEADERS );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment