Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - 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.

I create a custom field in the order section named delivery_date with a date value.

Now I wanted to use this custom field as a placeholder in email notification subject, like:

Your order is now {order_status}. Order details are shown below for your reference: 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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

To add a custom active placeholder {delivery_date} in woocommerce email subject, you will use the following hooked function.

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).

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). It should works.

Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {delivery_date}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...