dongwo5589
2016-09-28 05:33
浏览 84
已采纳

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

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('eee@hotmail.com', $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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试设置新订单时设置电子邮件地址。 我将新电子邮件存储在 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); 
 
 //将以下电子邮件替换为您想要的电子邮件
 $ emails = array('eeee @ hotmail.com',$ email); 
 
 
开关($ object)  {
 case'new_order':
 $ headers。='Bcc:'。  implode(',',$ email)。  “
 
”; 
 break; 
 case'customer_processing_order':
 $ headers。='密送:'。  implode(',',$ email)。  “
 
”; 
 break; 
 case'customer_completed_order':
 case'customer_invoice':
 $ headers。='Bcc:'。  implode(',',$ email)。  “
 
”; 
 break; 
 
默认值:
} 
 
返回$ headers; 
} 
 
add_filter('woocommerce_email_headers','techie_custom_wooemail_headers',10,2); \  n   
 
 

如何取回数据?

谢谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drtkyykai004574380 2016-09-28 08:01
    已采纳

    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 = '<name@email.com>';
        $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('eee@hotmail.com', $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.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题