【问题标题】:Adding email notification to site admin for custom order status change in Woocommerce向站点管理员添加电子邮件通知以在 Woocommerce 中更改自定义订单状态
【发布时间】:2021-12-13 07:46:45
【问题描述】:

WooCommerce 默认模式是在订单状态发生变化时向客户发送电子邮件。但是管理员/商店经理如何知道何时进行了这些更改?保持订单页面 24/7 全天候打开并刷新视图不是解决方案。

我已尝试以下代码,但这不适用于自定义订单状态。

add_filter('woocommerce_email_recipient_customer_processing_order', 'email_recipient_custom_notification', 10, 2);
function email_recipient_custom_notification( $recipient, $order ) {
                    
      if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
                    
      error_log("Hello: woocommerce_email_recipient_customer_processing_order : recipient = " . $recipient . "\n" );
                    
      // Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
      $recipient .= ', admin@xyz.com';
      return $recipient;
 }
                
add_filter('woocommerce_email_recipient_customer_out-to-delivery_order', 'email_out_to_delivery_notification', 10, 2);
function email_out_to_delivery_notification( $recipient, $order ) {
                    
      if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
                    
      error_log("Hello: woocommerce_email_recipient_customer_out-to-delivery_order : recipient
= " . $recipient . "\n" );
                    
      // Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
      $recipient .= ', admin@xyz.com';
      return $recipient;
}

请注意,'out-to-delivery' 是我已成功添加的自定义订单状态。我可以将订单状态从“暂停-->处理-->发货-->完成”更改。

通过上述更改,网站管理员会收到“新订单”和“处理中”订单状态的通知。但他没有收到有关“交货外”或“已完成”状态更改的通知。似乎我使用的过滤器'woocommerce_email_recipient_customer_out-to-delivery_order'不起作用。

谢谢

【问题讨论】:

  • 你可以试试这个钩子woocommerce_process_shop_order_meta
  • 您正在使用的钩子会为现有的电子邮件通知添加收件人,这些通知无论如何都会发送。除非您已有效提供此信息,否则不会针对自定义订单状态发送电子邮件。是这样吗? “请注意,'out-to-delivery' 是我已成功添加的自定义订单状态。” - 您能否详细说明如何添加此/这些,以便您的问题包含 a Minimal, Reproducible Example
  • 嗨@MainulHasan,当订单状态改变时,钩子'woocommerce_process_shop_order_meta'被调用。但我的目标是向商店管理员发送电子邮件通知。如何使用这个钩子来做到这一点?
  • @user2679476 以下答案包含您问题答案的 90% -> Add a new order status that Send an email notification in WooCommerce 4+ - 您唯一需要添加的是电子邮件也会发送给管理员,即已经适用于您的代码尝试
  • 嗨@7uc1f3r,事实上我试过这个链接。但是当我添加此行时,它会给出错误(此网站出现严重错误。请检查您的网站管理员电子邮件收件箱以获取说明。):- add_action( 'woocommerce_order_status_wc-out-to-delivery', array( WC() , 'send_transactional_email' ), 10, 1 );

标签: woocommerce hook-woocommerce


【解决方案1】:

以下代码适用于我发送有关自定义订单状态更改的电子邮件通知:

add_filter('woocommerce_email_recipient_customer_processing_order', 'email_recipient_custom_notification', 10, 2);
function email_recipient_custom_notification( $recipient, $order ) {
    
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    
    // Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
    $recipient .= ', admin@xyz.com';
    return $recipient;
}

add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );

function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
    
    $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
    
    if ($new_status === 'out-to-delivery') {

        $wc_emails['WC_Email_Customer_Processing_Order']->trigger( $order_id );
    }
   
}

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2018-01-04
    • 2019-01-12
    • 2017-06-14
    • 2016-03-14
    • 1970-01-01
    相关资源
    最近更新 更多