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

时间:2016-09-28 05:33:40

标签: php wordpress woocommerce orders hook-woocommerce

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

如何取回数据?

感谢。

2 个答案:

答案 0 :(得分:3)

  

已更新添加了与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 :(得分: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;