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

查看:117
本文介绍了如何在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) . "\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);

如何获取数据?

谢谢.

推荐答案

已更新:增加了与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) . "\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);

我还没有测试您的代码,但是您有正确的方式获取订单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天全站免登陆