2016-01-17 54 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

相关问题