Create additional variables/placeholders for Woocommerce email notifications subject

杀马特。学长 韩版系。学妹 提交于 2019-12-11 02:25:28

问题


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

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

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

回答1:


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

Code goes in function.php file of your active child theme (or active theme). Tested and works only for Woocommerce version 3.2 and up…



来源:https://stackoverflow.com/questions/50120643/create-additional-variables-placeholders-for-woocommerce-email-notifications-sub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!