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

3

我想在有新订单时设置电子邮件地址。我将新的电子邮件存储在wp_postmeta中。

使用woocommerce_email_headers时如何获取$order_id

我需要获取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);

我该如何恢复数据?

谢谢。

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的方法是正确的。


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;


1
这意味着 $object 可能有 $order_id 吗? - Capslock10

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接