在新订单电子邮件中显示自定义产品字段

3

我在产品中创建了一个名为course-date的自定义字段。我将其分配了一个日期,例如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:contact@thinkandgrowrichinstitute.com">contact@thinkandgrowrichinstitute.com</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' ); ?>

你是如何定义 $_product 的?你是如何将这段代码添加到电子邮件中的?在哪里添加的?请编辑你的问题并提供额外信息。 - helgatheviking
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - John B.
我能雇用你帮我完成这个吗?我无法弄清楚它,而且我需要尽快完成,请告诉我。 - John B.
根据您要添加的新内容的主题,我可能会建议创建一个新的电子邮件,当购买该项目时,仅发送有关课程的信息。这里非常适合使用跟进电子邮件。或者使用 WooCommerce Bookings 进行完整的预订系统。 - helgatheviking
2个回答

3
希望您从这个答案中了解更多,并增加对WooCommerce的知识。
对于您发送的“客户处理订单电子邮件”的代码片段。
系统无法在此处找到$ _product-&gt; 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>";
   }
}
?>

请告诉我们这是否对您解决问题有所帮助。


这很有道理,但我不知道在哪里添加它?我尝试将其添加到模板中的单独php标记中,但仍然无法正常工作...我修改了上面的代码..如果我朝着正确的方向前进,请告诉我?谢谢 - John B.
嗨,我已经修改了代码。现在你可以把代码放在“客户处理订单电子邮件”中的任何位置,以便在电子邮件中显示该行。 - Domain

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。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接