Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

php - Create additional variables/placeholders for Woocommerce email notifications subject

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 );
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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…


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...