email – Notify users if their subscription is expired with cron

Question

I want to notify users when their subscription is expired. I did setup a cron event with ‘member_notify_expired’ hook name, runs once daily.

I tried to achieve this with:

function notify_expired_subscription() {
    $users = get_users();
    foreach ($users as $user) {
        $user_name = get_user_meta( $user->ID, 'last_name' );
        $expired_date = get_user_meta( $user->ID, 'membership_expiry' ); // I need to format this because this is stored as an unix time (strtotime)
        $expired_date_formatted = date('Y-m-d H:i:s',$expired_date);
        $current_date = date('Y-m-d H:i:s');
        if($expired_date_formatted < $current_date){
            wp_mail(get_user_meta( $user->ID, 'email' ), 'Notify about expired subscription', 'Dear '.$user_name.'! <br> Your subscription is expired!');
        }

    }
}
add_action( 'member_notify_expired', 'notify_expired_subscription');

But this is sending an email for every member doesn’t matter if they still have subscription or not and the $user_name variable is “Array” in the email. What am I doing wrong?

0
benyo 1 year 2021-11-15T12:47:34-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse