首页 文章

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

提问于
浏览
2

我在Woocommerce中设置了自定义状态和自定义电子邮件 . 我想使用当前的电子邮件 WC_Email ,而不是当前状态作为电子邮件模板中的变量 .

我需要在电子邮件模板中有一些if语句 . 我没有使用订单状态来确保来自订单的电子邮件是否手动重新发送,它不会通过单独的电子邮件发送当前订单状态的数据 .

如何将 WC_Email 电子邮件ID作为Woocommerce中的变量回显?

1 回答

  • 5

    WooCommerce中不存在 wc_order_email 类或函数,因此我更新了您的问题 .

    您正在查看的是 $email 变量参数(WC_Email当前类型对象) . 它主要在模板和钩子的各处定义 .

    要将可用的当前电子邮件ID作为变量,您只需使用 $email_id = $email->id ...

    要获取自定义电子邮件的当前电子邮件ID,您应该使用此代码(仅适用于 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>'; 
    }
    

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


    获得自定义电子邮件通知的 correct email ID slug 后,您可以在以下任何挂钩上使用它(而不是覆盖电子邮件模板):

    woocommerce_email_header (2个参数: $email_heading$email
    woocommerce_email_order_details (4个参数: $order$sent_to_admin$plain_text$email
    woocommerce_email_order_meta (4个参数: $order$sent_to_admin27241227$email
    woocommerce_email_customer_details (4个参数: $order$sent_to_admin$plain_text$email
    woocommerce_email_footer (1参数: $email

    在这里 example of code 我只针对"New order"电子邮件通知:

    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>';
    }
    

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

    经过测试和工作 .

相关问题