Last active
November 30, 2022 13:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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