2017-07-17 46 views
1

在WooCommerce,我使用「WC領域廠房」插件創建一個'serial'自定義字段的自定義字段值。我需要在/woocommerce/emails/customer-completed-order.php模板文件中顯示其值。顯示在Woocommerce電子郵件通知模板

我曾嘗試使用:

echo get_post_meta(get_post()->ID, "wccaf_serial", true); 

但它不工作。

我做錯了什麼?

感謝

回答

0

您可以直接使用$order所有WC_Order方法在此電子郵件模板,以獲取訂單ID,這樣一來:

// Get the Order ID (WooCommerce retro-compatibility) 
$order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 

// Get "serial" custom field value 
$serial = get_post_meta($order_id, "wccaf_serial", true); 

// Display "serial" custom field value 
echo '<p>'.__('Serial', 'woocommerce') . $serial . '</p>'; 

請閱讀:Template Structure + Overriding Templates via a Theme


而不是重寫此模板,您可以使用任何可用例如像BLE鉤:

add_action('woocommerce_email_order_details', 'action_wc_email_order_details' 50, 4); 
function action_wc_email_order_details($order, $sent_to_admin, $plain_text, $email){ 
    // Get the Order ID (WooCommerce retro-compatibility) 
    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 

    // Get "serial" custom field value 
    $serial = get_post_meta($order_id, "wccaf_serial", true); 

    // Display "serial" custom field value 
    echo '<p>'.__('Serial', 'woocommerce') . $serial . '</p>'; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

的代碼測試和WooCommerce 2.6.x的工作或3+

+0

非常感謝您的回答!這真的很有用。但不幸的是,我仍然無法獲得理想的結果。我得到**文本(串行)+變量的空值**。 我再次檢查了字段_「序列號」的名稱和值,但它沒有幫助(我使用** admin字段**和**文本值**)。無論如何,我會盡力繼續。 謝謝。 – Fost

+0

@Fost你應該直接在數據庫wp_postmeta表中搜索「wccaf_serial」meta_key。如果你沒有得到它,然後搜索訂單ID(post_id),看看你是否得到任何「序列」meta_key ... – LoicTheAztec

+0

謝謝,我做了一個搜索,並得到了5個字段(也許我複製並刪除此字段進行驗證)與名稱**「wccaf_serial」**。第一個字段的值爲「meta_value」{「type」:「text」,「label」:「serial」,「name」:「serial」,「r ....其他字段爲空。 – Fost