【问题标题】:Send an Email notification to the admin for pending order status in WooCommerce向管理员发送电子邮件通知,了解 WooCommerce 中的待处理订单状态
【发布时间】:2018-01-04 14:53:57
【问题描述】:

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

我想就此类订单向管理员发送电子邮件。我该怎么做?

【问题讨论】:

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

更新 2 (从 woocommerce_new_order 更改为 woocommerce_checkout_order_processed 感谢 Céline Garel)

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

// 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 版本。
  • @burhanjamil 我已经更新了我的答案,请尝试一下。我已经改变了钩子,现在它应该在任何情况下都可以用于“挂单状态”……现在代码更有效、更紧凑、更轻量。有 2 个版本,一个只发送默认的“新订单”通知,另一个允许一些自定义(如果需要)......
  • 它不工作。我还通过创建 simple.txt 文件来测试钩子是否正在调用。它调用了该函数,但无法触发订单电子邮件。
  • 感谢您开箱即用。 WordPress 版本 5.2.2
  • 效果很好。谢谢你。我只有一个问题?我想在订单待处理且付款方式为信用卡时发送电子邮件。我怎么能这样做?
【解决方案2】:

我已尝试使用 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 修复了它,并且邮件按原样到达:-)
猜你喜欢
  • 2019-12-05
  • 2017-06-14
  • 2020-10-02
  • 2021-12-12
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
相关资源
最近更新 更多