Skip to content

Instantly share code, notes, and snippets.

@assoscoupa
Last active April 23, 2024 11:24
Show Gist options
  • Save assoscoupa/71833cc733cb6db91da48a32ff0c5368 to your computer and use it in GitHub Desktop.
Save assoscoupa/71833cc733cb6db91da48a32ff0c5368 to your computer and use it in GitHub Desktop.
Add custom placeholders to WooCommerce email subject and body
/**
* @snippet Add custom placeholders to WooCommerce email subject and body
* @author Paris Koutroumanos
* @website https://www.qwerty.gr/
* @compatible WooCommerce 3.2+
* Read more https://wpauthors.com/blog/how-to-add-custom-placeholders-to-woocommerce-email-subject/
* Read more https://www.businessbloomer.com/woocommerce-get-logged-username/
*/
function qwerty_filter_email_format_string( $string, $email ) {
// Get WC_Order object from email
$order = $email->object;
// Get Username
$current_user = wp_get_current_user();
// Add new placeholders
$new_placeholders = array(
'{_first_name}' => $order->get_billing_first_name(),
'{_order_total}' => $order->get_total(),
'{_payment_method}' => $order->get_payment_method_title(),
'{_username1}' => $current_user->user_login,
);
// return the string with new placeholder replacements
return str_replace( array_keys( $new_placeholders ), array_values( $new_placeholders ), $string );
}
add_filter( 'woocommerce_email_format_string' , 'qwerty_filter_email_format_string', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment