在WooCommerce中,对于待处理订单状态,向管理员发送电子邮件通知

8
在WooCommerce中,当客户从购物车结账并提交订单时,如果付款未处理,则该订单将被设置为“待处理”付款状态。管理员将不会收到任何有关此订单的电子邮件通知。
我想要发送一封电子邮件给管理员以获取这种类型的订单。我该如何操作?

WooCommerce有很多钩子,我相信其中一个可能会有所帮助:https://docs.woocommerce.com/wc-apidocs/hook-docs.html - WheatBeak
3个回答

23

更新2 (由于Céline Garel的建议,从woocommerce_new_order更改为woocommerce_checkout_order_processed

当一个新订单获得待处理状态时,此代码将在所有可能情况下触发,并自动触发“新订单”电子邮件通知:

// New order notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;

    // Send "New Email" notification (to admin)
    WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}

将代码放入您的当前子主题(或主题)的functions.php文件中,也可以放入任何插件文件中。


如果需要更多自定义版本的代码,将使“待处理订单”更加可见:

// New order notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;

    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - New customer Pending order ({order_number}) - {order_date}');

    // Change Heading
    $wc_email->settings['heading'] = __('New customer Pending Order'); 
    // $wc_email->settings['recipient'] .= ',name@email.com'; // Add email recipients (coma separated)

    // Send "New Email" notification (to admin)
    $wc_email->trigger( $order_id );
}

将代码放在您的活动子主题(或主题)的functions.php文件中,或者放在任何插件文件中。

此版本允许自定义电子邮件标题、主题、添加收件人等...


亲爱的,这个函数看起来非常棒,但是对我来说它不起作用。我正在使用 WP 4.8 版本。 - burhan jamil
@burhanjamil 我已更新我的答案,请尝试一下。我已更改钩子,现在它应该适用于任何“待处理订单状态”情况... 现在代码更有效、紧凑和轻量级。有两个版本,一个只发送默认的“新订单”通知,另一个则允许进行一些自定义(如果需要)... - LoicTheAztec
它不工作。我也测试了创建一个simple.txt文件,无论挂钩是否调用。它调用了函数但无法触发订单电子邮件。 - dineshkashera
感谢您的使用,Thank you可以直接使用。WordPress版本为5.2.2。 - Joost Pielage
非常好,谢谢。我只有一个问题?当订单处于待处理状态且付款方式为信用卡时,我想发送电子邮件。我该怎么做? - Miran Urbas
@LoicTheAztec,这段代码是触发了一次吗?还是每天触发一次? - Rosalito Udtohan

3

我尝试使用LoicTheAztec的答案,非常感谢你的出色代码@LoicTheAztec。

我只是将操作挂钩从woocommerce_new_order更改为woocommerce_checkout_order_processed以使其正常工作。

以下是操作:add_action('woocommerce_checkout_order_processed','pending_new_order_notification',20,1);

希望这可以帮助到您。


谢谢这个修复!当我使用 woocommerce_new_order 时,订单邮件没有包含产品信息,但是使用 woocommerce_checkout_order_processed 解决了这个问题,现在邮件已经正常到达 :-) - EranKT

0
感谢 @LoicTheAztec - 这是你的代码的一个版本,在状态为“待付款”10分钟后发送电子邮件。
它利用了动作调度程序,并避免了管理员在订单仅暂时处于该状态(例如外部信用卡支付)时收到待处理的电子邮件。
请注意过滤器 woocommerce_new_order_email_allows_resend,它允许多次发送新订单电子邮件。

add_filter( 'woocommerce_new_order_email_allows_resend', '__return_true' );

add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );

function pending_new_order_notification( $order_id ) {
    $delayTime = 600;

    as_schedule_single_action(time() + $delayTime, 'queue_pending_email', array( $order_id ), 'customActions');
}

add_action( 'queue_pending_email', function( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;
    
    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - New customer pending order ({order_number}) - {order_date}');

    // Change Heading
    $wc_email->settings['heading'] = __('New Order Still Pending'); 
    // $wc_email->settings['recipient'] .= ',name@email.com'; // Add email recipients (coma separated)

    $wc_email->settings['additional_content'] = __('This order is currently pending.');

    // Send "New Email" notification (to admin)
    $wc_email->trigger( $order_id );
}, 20, 1 );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接