为 Woocommerce 电子邮件通知主题创建其他变量/占位符 [英] Create additional variables/placeholders for Woocommerce email notifications subject

查看:20
本文介绍了为 Woocommerce 电子邮件通知主题创建其他变量/占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从自定义订单状态激活新电子邮件通知时,我为主题占位符提供了以下选项:

When activating new email notifications from Custom order statuses I have this options for Subject placeholders:

您可以使用以下占位符:{order_date}、{order_number}、{order_status}、{billing_first_name}、{billing_last_name}、{billing_company}、{blogname}、{site_title}"

"You can use the following placeholders: {order_date}, {order_number}, {order_status}, {billing_first_name}, {billing_last_name}, {billing_company}, {blogname}, {site_title}"

有没有办法为其他字段创建新的占位符?基本上,我创建了一个名为 test_pw 的新隐藏字段,我希望能够将 {billing_email} 和 {test_pw} 添加到我的自定义电子邮件中.

Is there a way of creating new placeholders for other fields? Basically, I have created a new hidden field called test_pw and I want to be able to add {billing_email} and {test_pw} to my custom emails.

我曾尝试使用好心建议的代码,但我不知道如何格式化它.

I have tried with this code kindly suggested but I am not sure how to format it.

// Only for woocommerce versions 3.2 + (up to 3.2)
add_filter( 'woocommerce_email_format_string' , 'filter_email_format_string', 20, 2 );
function filter_email_format_string( $string, $email ) {
// Get the instance of the WC_Order object
$order = $email->object;

// Additional wanted placeholders in the array of find / relace pairs
$additional_placeholders = array(
    '{custom_one}'      => __('my replacement one','woocommerce'),
    '{billing_email}'   => $order->get_billing_email(),
    '{test_pw}'   => $order->get_test_pw(),
);

// return the clean string with new replacements
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}

推荐答案

以下示例将向您展示如何为电子邮件通知添加新占位符 主题:

Here is an example that will show you how to add new placeholders for email notifications Subject:

// Only for woocommerce versions 3.2 + (up to 3.2)
add_filter( 'woocommerce_email_format_string' , 'filter_email_format_string', 20, 2 );
function filter_email_format_string( $string, $email ) {
    // Get the instance of the WC_Order object
    $order = $email->object;

    // Additional wanted placeholders in the array of find / relace pairs
    $additional_placeholders = array(
        '{custom_one}'      => __('my replacement one','woocommerce'),
        '{shipping_city}'   => $order->get_shipping_city(),
        '{yudu_pw}'         => $order->get_meta( 'yudu_pw' ), // <=== HERE
    );

    // return the clean string with new replacements
    return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中.经过测试并仅适用于 Woocommerce 3.2 及更高版本......

这篇关于为 Woocommerce 电子邮件通知主题创建其他变量/占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆