I am using a plugin to send custom referral codes in the email orders but now I found that every order email has the code inserted.
function gens_raf_customer_email( $order, $sent_to_admin, $plain_text ) {
$user_id = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->customer_user : $order->get_customer_id();
if( ! empty( $user_id ) && ( get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
$code = get_user_meta($user_id, "gens_referral_id", true);
} else {
$code = ( version_compare( WC_VERSION, '2.7', '<' ) ) ? $order->billing_email : $order->get_billing_email();
}
if( $plain_text ){
_e('Your referral code is: ','gens-raf') . $code;
} else {
echo '<p style="text-align:center;margin-top:10px;">Your referral code is: ' .get_home_url() .'?raf='. $code . '</p>';
}
}
add_action(‘woocommerce_email_customer_details’, ‘gens_raf_customer_email’, 30, 3 );
I was messing around with
if ( $email->id == 'customer_completed_order' )
But gave me error from the php code builder. The main issue is to send that email only to completed and/or processing orders instead of refunded, cancelled and so on.
Thank you!
2
Answers
You’re getting the error because you didn’t add the fourth
$email
argument available to thewoocommerce_email_customer_details
hook.Try this:
The code has been tested and works.
The variable
$email
is the 4th missing argument from your hooked function, that you need to target specific email notifications.Use instead (for woocommerce 3 and above):
Code goes in functions.php file of the active child theme (or active theme). It should works.