简体   繁体   English

在 WooCommerce 中为电子邮件主题添加自定义占位符

[英]Add a custom placeholder to email subject in WooCommerce

I have a Woocommerce shop and I wanted to add a delivery_date after I accept the payment.我有一家 Woocommerce 商店,我想在接受付款后添加一个delivery_date

I create a custom field in the order section named delivery_date with a date value.我在名为delivery_date的订单部分中创建了一个带有日期值的自定义字段。

Now I wanted to use this custom field as a placeholder in email notification subject, like:现在我想将此自定义字段用作电子邮件通知主题中的占位符,例如:

Your order is now {order_status}.您的订单现在是 {order_status}。 Order details are shown below for your reference: deliverydate: {delivery_date}订单详情如下供您参考: Deliverydate: {delivery_date}

I think the placeholder don't work like this, I need to change something in the php but I don't know where.我认为占位符不能像这样工作,我需要在 php 中更改一些内容,但我不知道在哪里。

To add a custom active placeholder {delivery_date} in woocommerce email subject, you will use the following hooked function.要在 woocommerce 电子邮件主题中添加自定义活动占位符{delivery_date} ,您将使用以下挂钩函数。

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 ) .您之前将检查, delivery_date是用于将结帐字段值保存到订单的正确帖子元键(在wp_postmeta数据库表中检查订单post_id

The code:编码:

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 );
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 It should works.它应该有效。

Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {delivery_date}然后在 Woocommerce > 设置 > 电子邮件 > “新订单”通知中,您将能够使用动态占位符{delivery_date} ...

If you want to print value of "delivery_date" in email content then you can do like this.如果您想在电子邮件内容中打印“delivery_date”的值,那么您可以这样做。

$content = "Your order is now %%order_status%%. Order details are shown below for your reference: deliverydate: %%delivery_date%%";
$search_array = ["{order_status}","{delivery_date}"]
$replace_array = [$valueOfOrderStatus,$valueOfDeliveryDate];
$content = str_replace($search_array, $replace_array, $content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM