php - 在 WooCommerce 中向电子邮件主题添加自定义占位符
问题描述
我有一家 Woocommerce 商店,我想delivery_date
在接受付款后添加一个。
delivery_date
我在以日期值命名的订单部分中创建了一个自定义字段。
现在我想将此自定义字段用作电子邮件通知主题中的占位符,例如:
您的订单现在是 {order_status}。订单详情如下所示供您参考:交货日期:{delivery_date}
我认为占位符不是这样工作的,我需要在 php 中更改一些内容,但我不知道在哪里。
解决方案
要在 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>
推荐阅读
- java - 无法让我的复制构造函数在我的简单代码中工作(我是 java 新手)
- r - 有没有办法使用 R 中的一个应用函数来优化这个循环?
- c - 字符串未显示在 C 的控制台中
- python - 简单的 Python 多线程网络服务器,带有 Asyncio 和在主函数中调用的事件
- java - 如何将最后两位小数打印成整数
- c# - Entity Framework Core 使用 CurrentValues.SetValues 从其他实体更新实体属性,由于实体上的主键而失败
- callback - Scrapy:如何从页面爬取并正确返回调用页面?“缺少 1 个必需的位置参数:‘响应’。”
- c++ - 在这种情况下,“必须具有类或枚举类型的参数”是什么意思?
- python - 合并具有相同名称的列 - Python
- amazon-web-services - 如何在项目中部署 AWS CLI,以便以编程方式使用它?