Thursday, February 8, 2024

[SOLVED] WordPress - Cron job not executed

Issue

I'm having some problems to schedule a custom cron job in a wordpress plugin. I have this code inside my class and is supposed to send multiple emails to different adresses and each email will have a different subject and message.


class GreetingTicketScheduler {   

    public function __construct()
    {
        register_activation_hook(__FILE__, [$this, 'schedule_orders_forward']);
        add_action('forward_orders', [$this, 'send_confirmed_orders']);

    }

    public function schedule_orders_forward()
    {
        // Schedules the event if it's NOT already scheduled.
        if(!wp_next_scheduled('forward_orders')){
            wp_schedule_event( time(), '5min', 'send_confirmed_orders', [], true);
        }
    }

    public function send_confirmed_orders()
    {
        global $wpdb;
        $table = $wpdb->prefix . 'order_codes';
        //
        $sql = $wpdb->prepare("SELECT * FROM $table");
        //
        $results = $wpdb->get_results($sql, ARRAY_A);
        //
        if( !empty($results) ){
            //
            $pdv_subject = "Morning greetings of " . date('d-m-Y', time());
            //
            $pdv_message_a .= "Salut!\n";

            $pdv_email_a = '[email protected]';
            $pdv_headers_a[] = 'Cc: [email protected]';

            //
            $pdv_message_b .= "Ciao!\n";

            $pdv_email_b = '[email protected]';
            $pdv_headers_b[] = 'Cc: [email protected]';
            
            //
            $pdv_message_p .= "Hi!\n";

            $pdv_email_p = '[email protected]';
            $pdv_headers_p[] = 'Cc: [email protected]';


            //
            foreach( $results as $key => $val ){
                if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['order_cpco'] === '6605'){
                    $pdv_message_a .= $val['order_file'] . "\n";
                } 
                //
                if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['order_cpco'] === '6200'){
                    $pdv_message_b .= $val['order_file'] . "\n";
                }
                // 
                if(date('d-m-Y', $val['confirmed_at']) === date('d-m-Y', time()) && $val['order_cpco'] === '6600' ){
                    $pdv_message_p .= $val['order_file'] . "\n";
                }
            }            
            //
            //wp_mail( $to:string|array, $subject:string, $message:string, $headers:string|array, $attachments:string|array )
            //
            wp_mail($pdv_email_a, $pdv_subject, $pdv_message_a, $pdv_headers_a);
            //
            wp_mail($pdv_email_b, $pdv_subject, $pdv_message_b, $pdv_headers_b);
            //
            wp_mail($pdv_email_p, $pdv_subject, $pdv_message_p, $pdv_headaers_p);

        }        
    }
}

In my staging envoirment I have the plugin activated but I've noticed that the emails will be not send, probably I've missed something in the code.

What I need to modify to correctly schedule the cron job every five minutes or every hour?


Solution

You need to use an action hook to schedule the cron job, and then call the function.

class GreetingTicketScheduler {   

    public function __construct()
    {
        add_action('wp', [$this, 'schedule_orders_forward']);
        add_action('forward_orders', [$this, 'send_confirmed_orders']);
    }

    public function schedule_orders_forward()
    {
        if (!wp_next_scheduled('forward_orders')) {
            wp_schedule_event(time(), 'hourly', 'forward_orders');
        }
    }

    public function send_confirmed_orders()
    {
        // ... code to send emails ...
    }
}


Answered By - AR Riyad
Answer Checked By - Mildred Charles (WPSolving Admin)