3

我正在尝试在有新订单时设置电子邮件地址。我存储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);

如何取回数据?

谢谢。

4

2 回答 2

8

更新添加了与 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);

所以这是你的代码:

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。

于 2016-09-28T08:01:25.673 回答
1

在 WooCommerce 2.3 及更高版本中,他们更改了传递给过滤器的参数数量

function techie_custom_wooemail_headers( $headers, $id, $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($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);

$object- 表示此电子邮件用于,例如客户、产品或电子邮件。

尝试var_dump($object); exit;内部过滤回调。

于 2016-09-28T05:44:42.633 回答