如何在 woocommerce_email_headers 挂钩中获取订单 ID [英] How to get order ID in woocommerce_email_headers hook

查看:32
本文介绍了如何在 woocommerce_email_headers 挂钩中获取订单 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在有新订单时设置电子邮件地址.我将 new email 存储在 wp_postmeta 中.

I am trying to set the email address when have a new order. And I stored the new email in wp_postmeta.

使用woocommerce_email_headers时如何获取$order_id?

我需要获取 order_id 以将其与 get_post_meta() 函数一起使用.

I need to get the order_id to use it with get_post_meta() function.

这是我的代码:

function techie_custom_wooemail_headers( $headers, $object) {

    $email = get_post_meta( $order_id, '_approver_email', true );

    // Replace the emails below to your desire email
    $emails = array('eee@hotmail.com', $email);


    switch($object) {
        case 'new_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 2);

我如何取回数据?

谢谢.

推荐答案

更新:增加了与 Woocommerce 3+ 版的兼容性

我做了一些测试,试图从 $order 对象输出原始数据,但没有成功.经过一些其他测试,我现在得到了正确的订单 ID.我已经使用下面的代码进行测试以确保.将 $your_email 的值替换为您自己的电子邮件.然后您将收到一封电子邮件,标题中包含订单 ID:

I have made some tests trying to output raw data from $order object without success. After some other tests I got now the correct order ID. I have use the code below for my test to be sure. Replace the value of $your_email by your own email. Then you will receive an email with the order ID in the header name:

function testing_hook_headers( $headers, $id, $order ) {
    // The order ID | Compatibility with WC version +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    $your_email = '<name@email.com>';
    $headers = "To: Order Num $order_id $your_email";
    return $headers;
}
add_filter( 'woocommerce_email_headers', 'testing_hook_headers', 10, 3);

这是你的代码:

function techie_custom_wooemail_headers( $headers, $email_id, $order ) {

    // The order ID | Compatibility with WC version +3
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    $email = get_post_meta( $order_id, '_approver_email', true );

    // Replace the emails below to your desire email
    $emails = array('eee@hotmail.com', $email);

    switch( $email_id ) {
        case 'new_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 3);

我没有测试你的代码,因为它很特别,但你有正确的方式来获取订单 ID.

I havent test your code as it's particular, but you have the right manner to get order ID.

这篇关于如何在 woocommerce_email_headers 挂钩中获取订单 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆