Skip to content

Instantly share code, notes, and snippets.

@nicomollet
Last active November 30, 2022 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicomollet/a1285ae427d7b8b8845c3bb08456cbfd to your computer and use it in GitHub Desktop.
Save nicomollet/a1285ae427d7b8b8845c3bb08456cbfd to your computer and use it in GitHub Desktop.
WooCommerce Mail Params: customize "To" recipient with billing firstname and lastname along to the email for better email delivrability
<?php
/**
* WooCommerce: override mail params
* @param $params
* @param WC_Email $email
*
* @return mixed
*/
public function custom_woocommerce_mail_callback_params( $params, \WC_Email $email ){
$recipient = $params[0];
$before_recipient = '';
// Only do this if recipient is an email only, and just for customer emails (not admin emails)
if ( is_email( $recipient ) && $email->object instanceof WC_Order && str_contains( $email->id, 'customer' ) ) {
if ( method_exists( $email->object, 'get_billing_first_name' ) && method_exists( $email->object, 'get_billing_last_name' ) ) {
$before_recipient = $email->object->get_billing_first_name() . ' ' . $email->object->get_billing_last_name();
}
}
// Complete recipient if not empty
if ( $before_recipient != '' ) {
$params[0] = $before_recipient . ' <' . $recipient . '>';
}
return $params;
}
add_filter('woocommerce_mail_callback_params', 'custom_woocommerce_mail_callback_params', 20, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment