在 WooCommerce 中为电子邮件主题添加自定义占位符 [英] Add a custom placeholder to email subject in WooCommerce
问题描述
我有一家 Woocommerce 商店,我想在接受付款后添加 delivery_date
.
I have a Woocommerce shop and I wanted to add a delivery_date
after I accept the payment.
我在名为 delivery_date
的订单部分创建了一个带有日期值的自定义字段.
I create a custom field in the order section named delivery_date
with a date value.
现在我想将此自定义字段用作电子邮件通知主题中的占位符,例如:
Now I wanted to use this custom field as a placeholder in email notification subject, like:
您的订单现在是 {order_status}.订单详情如下供您参考:deliverydate: {delivery_date}
Your order is now {order_status}. Order details are shown below for your reference: deliverydate: {delivery_date}
我认为占位符不能像这样工作,我需要在 php 中更改一些内容,但我不知道在哪里.
I think the placeholder don't work like this, I need to change something in the php but I don't know where.
推荐答案
要在 woocommerce 电子邮件主题中添加自定义活动占位符 {delivery_date}
,您将使用以下挂钩函数.
To add a custom active placeholder {delivery_date}
in woocommerce email subject, you will use the following hooked function.
您之前会检查,delivery_date
是用于将结帐字段值保存到订单的正确后元键(在 wp_postmeta
数据库表中检查订单post_id
).
You will check before, that delivery_date
is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta
database table for the order 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 文件中.它应该有效.
Code goes in function.php file of your active child theme (or active theme). It should works.
然后在 Woocommerce > 设置 > 电子邮件 > 新订单"通知中,您将能够使用动态占位符 {delivery_date}
...
Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {delivery_date}
…
这篇关于在 WooCommerce 中为电子邮件主题添加自定义占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!