2016-09-28 75 views
1

我試圖在訂購新訂單時設置電子郵件地址。並且我將new email存儲在wp_postmeta中。如何在woocommerce_email_headers掛鉤中獲取訂單ID

如何在使用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('[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); 

如何找回數據?

謝謝。

回答

3

我做了一些測試,試圖從$ order對象輸出原始數據而沒有成功。經過一些其他測試後,我得到了正確的訂單ID。我已經使用下面的代碼進行測試了。用您自己的電子郵件替換$your_email的值。然後你會收到一封電子郵件,在頭名的順序編號:

function testing_hook_headers($headers, $id, $order) { 
    $order_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, $id, $order) { 

    // The order ID 
    $order_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($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, 3); 

我還沒有測試你的代碼,因爲它是特定的,但你有正確的方式獲取訂單ID。

+0

這是工作。謝謝。 – Capslock10

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('[email protected]', $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;裏面的過濾器回調。

+0

就是這個意思,'$ object'可能有'$ order_id'? – Capslock10

+0

是的,請嘗試打印'$ object'數組。 –