编程语言
首页 > 编程语言> > php – 向管理员发送电子邮件通知,告知WooCommerce中的待处理订单状态

php – 向管理员发送电子邮件通知,告知WooCommerce中的待处理订单状态

作者:互联网

在WooCommerce中,当客户从购物车结账并提交订单时,如果未处理付款,则订单将设置为“待处理”付款.管理员未收到任何有关的电子邮件.

我想向管理员发送电子邮件以获取此类订单.我该怎么做?

解决方法:

更新2(感谢CélineGarel,从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 );
}

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

此代码经过测试,适用于WooCommerce版本2.6.x和3.

一个更可自定义的代码版本(如果需要),这将使待定订单更加可见:

// 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 );
}

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

此代码经过测试,适用于WooCommerce版本2.6.x和3.

In this version you can customize the email heading, subject, add recipients…

标签:orders,php,wordpress,woocommerce,email-notifications
来源: https://codeday.me/bug/20190724/1522679.html