如何在woocommerce_email_headers挂钩中获取订单ID - php

我有新订单时尝试设置电子邮件地址。然后将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('[email protected]', $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:

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);

所以这是您的代码:

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) . "\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。

为什么我的代码针对特定域测试电子邮件地址不起作用? - php

我只想允许特定的电子邮件域。其实我做到了。我想问的是为什么我的第一个代码根本不起作用。我只是想学习PHP,以便使这个问题看起来很愚蠢,对此感到抱歉。这是我的代码:function check_email_address($email) { $checkmail = print_r (explode("@",$email)); $conta…

laravel使用gmail无法在服务器上发送邮件 - php

嗨,在我的laravel应用程序中,当我创建新用户时,我正在发送邮件!所以在本地主机中,我用我的gmail帐户和用户名使用gmail smtp,并且可以正常工作,但是在服务器上却收到此错误Swift_TransportException Expected response code 250 but got code "535", with…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…