如何在woocommerce_email_headers挂钩中获取订单ID

Cap*_*k10 3 php wordpress orders woocommerce hook-woocommerce

我有新订单时尝试设置电子邮件地址。然后将储存在new emailwp_postmeta

$order_id使用时如何获得woocommerce_email_headers

我需要order_id使用它与get_post_meta()功能。

这是我的代码:

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) . "\r\n";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "\r\n";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "\r\n";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 2);
Run Code Online (Sandbox Code Playgroud)

如何获取数据?

谢谢。

Loi*_*tec 5

更新添加了与Woocommerce版本3+的兼容性

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

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);
Run Code Online (Sandbox Code Playgroud)

所以这是您的代码:

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) . "\r\n";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "\r\n";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "\r\n";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 3);
Run Code Online (Sandbox Code Playgroud)

我还没有测试您的代码,但是您有正确的方式获取订单ID。