doulao3078 2017-12-10 17:38
浏览 81
已采纳

在Woocommerce电子邮件中获取电子邮件ID

I have custom statuses and custom emails set up in Woocommerce. I would like to use the current email, WC_Email, not the current status as a variable inside email templates.

I need to have some if statements in the email templates. I am not using the order status to ensure if an email from an order gets resent manually it doesn't send data for the current order status with an separate email.

How can I echo the WC_Email email ID as a variable in Woocommerce?

  • 写回答

1条回答 默认 最新

  • dongmibeng5885 2017-12-10 19:49
    关注

    The wc_order_email class or function doesn't exist in WooCommerce, so I have updated your question.

    What you are looking at is $email variable argument (the WC_Email current type object). It's mostly defined everywhere in templates and hooks.

    To get the usable current Email ID as a variable you will simply use $email_id = $email->id

    To get the current Email ID of your custom emails, you should use this code (just for testing):

    add_action( 'woocommerce_email_order_details', 'get_the_wc_email_id', 9, 4 );
    function get_the_wc_email_id( $order, $sent_to_admin, $plain_text, $email ) {
        // Will output the email id for the current notification
        echo '<pre>'; print_r($email->id); echo '</pre>'; 
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


    Once you get the correct email ID slug for your custom email notification you can use it on any following hook (instead of overriding email templates):

    woocommerce_email_header (2 arguments: $email_heading, $email)
    woocommerce_email_order_details (4 arguments: $order, $sent_to_admin, $plain_text, $email)
    woocommerce_email_order_meta (4 arguments: $order, $sent_to_admin, $plain_text, $email)
    woocommerce_email_customer_details (4 arguments: $order, $sent_to_admin, $plain_text, $email)
    woocommerce_email_footer (1 argument: $email)

    HERE an example of code where I target "New order" email notifications only:

    add_action( 'woocommerce_email_order_details', 'add_custom_text_to_new_order_email', 10, 4 );
    function add_custom_text_to_new_order_email( $order, $sent_to_admin, $plain_text, $email ) {
        // Only for "New Order"  email notifications (to be replaced by yours)
        if( ! ( 'new_order' == $email->id ) ) return;
    
        // Display a custom text (for example)
        echo '<p>'.__('My custom text').'</p>';
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?