2016-01-17 58 views
3

我在產品中創建了一個名爲課程日期的自定義字段。我認爲它是一個日期,例如1月30日。這是我在電子郵件中沒有任何顯示..我錯過了什麼?代碼與新代碼片段編輯下面以新訂單顯示自定義產品字段電子郵件

enter image description here

<?php 
/** 
* Customer processing order email 
* 
* @author  WooThemes 
* @package  WooCommerce/Templates/Emails 
* @version  2.4.0 
*/ 

if (! defined('ABSPATH')) { 
    exit; // Exit if accessed directly 
} 

?> 

<?php 
$items = $order->get_items(); 
foreach ($items as $item) { 
    $product_name = $item['name']; 
    $product_id = $item['product_id']; 
    $product_variation_id = $item['variation_id']; 
    $course_date=get_post_meta($product_id, 'course-date', true); 
    //Note $course_date will get overwritten if there are more than one items in the order. 
} 
?> 

<?php do_action('woocommerce_email_header', $email_heading); ?> 

<p>This email is to notify you of your recent registration for the <b>Level 1 - Think and Grow Rich Institute Course</b>!</p> 

<p>You are registered to attend on:</p> 

<p><?php printf(__('<b>Registration ID:</b> %s', 'woocommerce'), $order->get_order_number()); ?></p> 
<p><b>Course Registration Date:</b> <?php echo get_post_meta($post->id, 'course-date', true); ?></p> 
<p><b>Time:</b> 8am Registration - 9am Event Begins (<a href="http://allfansnomoney.com/thinkandgrowrich/tentative-schedule/" target="_blank">See Full Schedule</a>)</p> 
<p><b>Location:</b> 240 Belfield Rd, Toronto, ON M9W 1H3</p> 

<p>If you have any questions please contact us by e-mail: <a href="mailto:[email protected]">[email protected]</a>.</p> 

<p>Sincerely,<BR/> 
The Think and Grow Rich Institute Team</p><BR/> 

<p><em><b>"Whatever The Mind Can Conceive and Believe, It Will Achieve."</b> - Napoleon Hill</em></p> 

<?php do_action('woocommerce_email_before_order_table', $order, $sent_to_admin, $plain_text); ?> 

<?php do_action('woocommerce_email_after_order_table', $order, $sent_to_admin, $plain_text); ?> 

<?php do_action('woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text); ?> 

<?php do_action('woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text); ?> 

<?php do_action('woocommerce_email_footer'); ?> 
+0

你如何定義'$ _product'?你如何將此代碼添加到電子郵件?哪裏?請編輯您的問題與額外的信息。 – helgatheviking

+0

我已編輯幷包含完整的電子郵件模板。通過定義$ _product,我假設我必須添加一個片段或東西到functions.php?對不起還在學習 –

+0

我可以僱用你爲我完成這件事嗎?我無法弄清楚,我需要儘快完成,請讓我知道。 –

回答

3

希望你能學到這個答案更並將其添加到您WooCommerce的知識。

對於您發送的「客戶處理訂單電子郵件」的代碼段。

此處系統無法找到$ _product-> id的值。所以它不起作用。

您必須通過在初始測試電子郵件中打印產品ID本身來進行調試。

如果您無法獲取產品ID。這裏有更多的信息。

訂單可能有多個產品。因此,找到帶有元字段的產品可能會很麻煩,特別是當您遇到某個產品應顯示元字段的問題時。

簡單的方法是使用給定的對象$ order從該對象中檢索訂單ID,然後查找所有與此訂單關聯的產品,然後找到這些產品的元字段值。

<?php 
$items = $order->get_items(); 
foreach ($items as $item) { 
    $product_name = $item['name']; 
    $product_id = $item['product_id']; 
    $product_variation_id = $item['variation_id']; 
    $course_date=get_post_meta($product_id, 'course-date', true); 
    if(isset($course_date) && $course_date!=""){ 
     echo "<p><b>Course Registration Date:</b>".$course_date."</p>"; 
    } 
} 
?> 

讓我們知道這是否讓你對這個問題有所瞭解。

+0

這是有道理的,但我不知道我在哪裏添加這個?我試圖將它添加到模板是單獨的PHP標記,但它仍然無法正常工作..我修改了上面的代碼..讓我知道如果我正朝着正確的方向請求?謝謝 –

+0

嗨,修改了代碼。現在,您只需將代碼放置在「客戶處理訂單電子郵件」中,無論您希望在電子郵件中顯示哪一行。 – WisdmLabs

2

以下是未經測試的如何將字符串添加到「customer_processing_email」而不覆蓋模板的示例。您可以將此代碼添加到主題的functions.php,並且文本應該顯示在訂單表格之前。

add_action('woocommerce_email_order_details', 'kia_custom_order_details', 5, 4); 
function kia_custom_order_details($order, $sent_to_admin, $plain_text, $email){ 

    if($email->id == "customer_processing_order"){ 

     $course_date = null; 

     // borrowing the loop from WisdmLabs's answer 
     $items = $order->get_items(); 

     foreach ($items as $item) { 
      $product_name = $item['name']; 
      $product_id = $item['product_id']; 
      $product_variation_id = $item['variation_id']; 
      $course_date = get_post_meta($product_id, 'course-date', true); 
      if($course_date != '') break; //break out of the loop if we find a course date 
     } 

     if($course_date && $plain_text){ 

      echo "Course Registration Date: " . $course_date . "\n\n"; 

     } else if($course_date && ! $plain_text){ 

      echo "<p><b>Course Registration Date:</b> " . $course_date . "</p>" . "/n/n"; 

     } 

    } 
} 

不要忘記豐富文本與純文本電子郵件的不同格式。我將其餘大部分文本的格式留給了你,因爲重要的部分是查找/打印課程日期元。

我仍然認爲使用單獨的電子郵件將是最好的方法,但據我所知,只有在正確的地方遺漏了$course_date

相關問題