以编程方式重新发送WooCommerce customer_on_hold_order电子邮件通知
问题描述:
我注意到客户暂停订单电子邮件不可用,因此我试图用一个操作替换发送相应电子邮件的操作。以编程方式重新发送WooCommerce customer_on_hold_order电子邮件通知
这似乎工作,除了保持状态。我没有看到持有和处理案例之间的区别是什么,除了$available_emails
,class-wc-meta-box-order-actions.php
之外,我已经删除了所有其他的,他们仍然工作。
我做错了什么?这是否可以使这成为可能?
我的代码是:
function ulmh_resend1($actions) {
$actions['ulmh_resend'] = __('Resend Email', 'text_domain');
return $actions;
}
function ulmh_resend2($order) {
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if ($order->has_status('on-hold')) {
$eml = 'customer_on_hold_order';
}elseif ($order->has_status('processing')) {
$eml = 'customer_processing_order';
}elseif ($order->has_status('completed')) {
$eml = 'customer_completed_order';
} else {
$eml = "nothing";
}
if (! empty($mails)) {
foreach ($mails as $mail) {
if ($mail->id == eml) {
$mail->trigger($order->id);
}
}
}
}
function ulmh_resend3($order_emails) {
$remove = array('new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice');
$order_emails = array_diff($order_emails, $remove);
return $order_emails;
}
add_action('woocommerce_order_actions', 'ulmh_resend1');
add_action('woocommerce_order_action_ulmh_resend', 'ulmh_resend2');
add_filter('woocommerce_resend_order_emails_available', 'ulmh_resend3');
答
我已经重新审视和压缩你的代码,因为那里有一些错误,如为eml
作为变量名中if ($mail->id == eml){
一个错字错误...此外,从获得订单ID WC_Order
您应该使用的对象$order->get_id()
方法而不是$order->id
。
下面是这个新的功能代码:
add_action('woocommerce_order_actions', 'ulmh_resend1');
function ulmh_resend1($actions) {
$actions['ulmh_resend'] = __('Resend Email', 'text_domain');
return $actions;
}
add_action('woocommerce_order_action_ulmh_resend', 'ulmh_resend2');
function ulmh_resend2($order) {
$wc_emails = WC()->mailer()->get_emails();
if(empty($wc_emails)) return;
if ($order->has_status('on-hold'))
$email_id = 'customer_on_hold_order';
elseif ($order->has_status('processing'))
$email_id = 'customer_processing_order';
elseif ($order->has_status('completed'))
$email_id = 'customer_completed_order';
else
$email_id = "nothing";
foreach ($wc_emails as $wc_mail)
if ($wc_mail->id == $email_id)
$wc_mail->trigger($order->get_id());
}
add_filter('woocommerce_resend_order_emails_available', 'ulmh_resend3');
function ulmh_resend3($order_emails) {
$remove = array('new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice');
$order_emails = array_diff($order_emails, $remove);
return $order_emails;
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
这个代码在WooCommerce 3+测试,现在工作得很好的保持订单状态的电子邮件通知,重新发送
+0
感谢Loic - 我的两个愚蠢的错误。需要更多的咖啡! –
有什么问题的时候,特别是?你的代码中有什么/不起作用? –
如果订单处于处理或已完成状态,但代码处于正常状态,但处于暂挂状态时未发送电子邮件,则代码可以正常工作。没有消息出现在调试日志中,它看起来好像customer_on_hold_order不在$邮件中,但原始电子邮件发送正确 –