READ FIRST: This code needs to be added at the end of the functions.php file. If you are familiar with editing functions.php, feel free to continue reading. If you haven’t done this before, adding this code in the wrong place might bring the site down and the only way to access it is by editing functions.php file again via FTP. Feel free to contact us if you need help!
If you do not have guest referral codes enabled, add this:
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 ) && ( $code = get_user_meta($user_id, "gens_referral_id", true) ) != '' ){
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>';
echo '<p style="text-align:center;margin-top:10px;">Share it with your friends:</p>';
echo '<p style="text-align:center;margin-top:10px;">Share on <a href="http://www.facebook.com/share.php?u='.get_home_url() .'?raf='. $code .'">Facebook.</a> Share on <a href="https://twitter.com/intent/tweet?url='.get_home_url() .'?raf='. $code .'">Twitter.</a>. </p>';
}
}
}
add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 3 );
If you have guest referral codes enabled, use this one:
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>';
echo '<p style="text-align:center;margin-top:10px;">Share it with your friends:</p>';
echo '<p style="text-align:center;margin-top:10px;">Share on <a href="http://www.facebook.com/share.php?u='.get_home_url() .'?raf='. $code .'">Facebook.</a> Share on <a href="https://twitter.com/intent/tweet?url='.get_home_url() .'?raf='. $code .'">Twitter.</a>. </p>';
}
}
add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 3 );
Once you add this code, the referral link should be shown at the bottom of every order email, as shown in the image below.