WordPress / WooCommerce:在扩展WC_Email的类中,cronjob不触发函数



我现在真的很沮丧,因为我似乎就是不知道这里出了什么问题。也许是一些非常简单的事情,我只是被困在了某个地方。请帮帮我!

我正在制作一个小插件来添加支付提醒到WooCommerce。我在激活钩子中添加了一个wp_schedule_event(nd_payment_reminder)

在我的主插件文件中,我包含了我的类,它看起来有点像这样:

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
}
[...]
}

我使用add_filter('woocommerce_email_classes', 'nd_add_payment_reminder_mails');来实例化类:

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');

无论我尝试,功能getOpenOrders似乎没有开火。我最初使用这个教程来编写这个插件:https://www.ibenic.com/create-custom-woocommerce-email/

我也在另一个网站上使用了相同的过程-它在那里工作!?

我甚至认为getOpenOrders()可能是错误的,但是当我尝试并使用另一个简单的函数从我的类它只是不工作。: - (

我已经被告知这是可能的,我正在使用的过滤器只在处理电子邮件时触发。但后来我很困惑为什么它实际上在不同的网站上工作。

任何想法都将非常感谢!提前感谢!

好吧,睡了一个好觉之后,我试了一个不同的方法。

我拿了这个:

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

,并将其放入插件的主文件中。这样的

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();
[...]
}

现在它按预期工作了。不知道为什么它在另一个网站上工作,但我可能也会在那里改变它。

谢谢你的阅读,也许它能帮助别人。

最新更新