yourwebseed
yourwebseed

Reputation: 51

WordPress / WooCommerce: cronjob not firing function within class that extends WC_Email

I'm really frustrated right now, because I just can't seem to figure out what's wrong here. Maybe it's something really simple and I'm just stuck somewhere. Please help me out!

I'm working on a small plugin to add a payment reminder to WooCommerce. I added a wp_schedule_event(nd_payment_reminder) to the activation hook. This works so far.

In my main plugin file I'm including my class which looks a little like this:

class ND_Payment_Due extends WC_Email
{
public function __construct() {
        parent::__construct();
        add_action('nd_payment_reminder', array($this, 'getOpenOrders'));
}

public function getOpenOrders() {
        // getting data and triggering the email
}
[...]
}

I'm using add_filter('woocommerce_email_classes', 'nd_add_payment_reminder_mails'); to instantiate the class like that:

function nd_add_payment_reminder_mails($email_classes)
{
    require('inc/class-wc-payment-due.php');
    $email_classes['ND_Payment_Due'] = new ND_Payment_Due();
    return $email_classes;
}
add_filter('woocommerce_email_classes', 'nd_add_payment_reminder_mails');

Whatever I try, the function getOpenOrders seems not to be firing. I've originally used this tutorial to write this plugin: https://www.ibenic.com/create-custom-woocommerce-email/

I've also used the same procedure on another site - and it works there!?

I even thought the getOpenOrders() might be faulty, but when I tried and used another simple function from within my class it's just not working. :-(

I was already told that it might be possible, that the filter I'm using is only fired when emails are handled. But then I'm confused why it actually works on a different site.

Any ideas would be highly appreciated! Thanks in advance!

Upvotes: 2

Views: 423

Answers (1)

yourwebseed
yourwebseed

Reputation: 51

Okay, after a good night sleep I tried a different approach.

I took this:

add_action('nd_payment_reminder', array($this, 'getOpenOrders'));

from the class and put it into the main file of the plugin. Like this

add_action('nd_payment_reminder', 'nd_send_reminder');

function nd_send_reminder()
{
    WC()->mailer(); // found out I have to use this to make sure the WC_Email class is instantiated
    [...]
    $nd_payment_reminder = new ND_Payment_Due();
    $nd_payment_reminder->getOpenOrders();
    [...]
}

Now it's working as expected. No idea why it is working on the other site, but I will probably change it over there as well.

Thanks anyways for reading - and maybe it helps someone else.

Upvotes: 3

Related Questions