首先,我们需要获取电子邮件 ID 以定位所有电子邮件通知。唯一的方法是之前获取它并在全局变量中设置该值。
然后在 WordPress 中挂钩的自定义函数中gettext
操作挂钩,我们可以更改(翻译)所有电子邮件通知中的“产品”。
这是该代码:
## Tested on WooCommerce 2.6.x and 3.0+
// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
$GLOBALS['email_id_str'] = $email->id;
}
add_filter('gettext', 'wc_renaming_email_label', 50, 3);
function wc_renaming_email_label( $translated_text, $untranslated_text, $domain ) {
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
if( !is_admin() && $email_id ) {
if( $untranslated_text == 'Product' )
$translated_text = __( 'Ticket', $domain );
}
return $translated_text;
}
此代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
此代码在 WooCommerce 2.6.x 到 3.0+ 上进行了测试并且有效。