首页 > 解决方案 > 在 WooCommerce 中向电子邮件主题添加自定义占位符

问题描述

我有一家 Woocommerce 商店,我想delivery_date在接受付款后添加一个。

delivery_date我在以日期值命名的订单部分中创建了一个自定义字段。

现在我想将此自定义字段用作电子邮件通知主题中的占位符,例如:

您的订单现在是 {order_status}。订单详情如下所示供您参考:交货日期:{delivery_date}

我认为占位符不是这样工作的,我需要在 php 中更改一些内容,但我不知道在哪里。

标签: phpwordpresswoocommerceordersemail-notifications

解决方案


要在 woocommerce 电子邮件主题中添加自定义活动占位符{delivery_date},您将使用以下挂钩函数。

您将在之前检查,这delivery_date是用于将结帐字段值保存到订单的正确后元键(在wp_postmeta数据库表中检查订单post_id

编码:

add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 10, 2 );
function add_custom_email_format_string( $string, $email ) {
    $meta_key    = 'delivery_date'; // The post meta key used to save the value in the order
    $placeholder = '{delivery_date}'; // The corresponding placeholder to be used
    $order = $email->object; // Get the instance of the WC_Order Object
    $value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : ''; // Get the value

    // Return the clean replacement value string for "{delivery_date}" placeholder
    return str_replace( $placeholder, $value, $string );
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。它应该有效。

然后在 Woocommerce > 设置 > 电子邮件 > “新订单”通知中,您将能够使用动态占位符{delivery_date}...</p>


推荐阅读