Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

php - How to get order ID in woocommerce_email_headers hook

I am trying to set the email address when have a new order. And I stored the new email in wp_postmeta.

How to get the $order_id when using woocommerce_email_headers?

I need to get the order_id to use it with get_post_meta() function.

Here is my code:

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('[email protected]', $email);


    switch($object) {
        case 'new_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 2);

How do I get back the data?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Updated: Added compatibility with Woocommerce version 3+

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 = '<[email protected]>';
    $headers = "To: Order Num $order_id $your_email";
    return $headers;
}
add_filter( 'woocommerce_email_headers', 'testing_hook_headers', 10, 3);

So Here is your code:

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('[email protected]', $email);

    switch( $email_id ) {
        case 'new_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_processing_order':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;
        case 'customer_completed_order':
        case 'customer_invoice':
            $headers .= 'Bcc: ' . implode(',', $emails) . "
";
            break;

        default:
    }

    return $headers;
}

add_filter( 'woocommerce_email_headers', 'techie_custom_wooemail_headers', 10, 3);

I havent test your code as it's particular, but you have the right manner to get order ID.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...