Use these snippets to add referral information to WooCommerce’s own order emails: the customer’s referral link in their order emails, and the advocate’s name in the admin New order email. Read Add code snippets safely first. Both are checked with version 4.4.12.
Add the referral link to customer order emails
This adds a line with the customer’s referral link to the Completed order email. Add more email IDs to $emails, such as customer_processing_order, to show it in other emails too.
add_action( 'woocommerce_email_customer_details', function ( $order, $sent_to_admin, $plain_text, $email ) {
$emails = array( 'customer_completed_order' );
if ( $sent_to_admin || ! class_exists( 'WPGens_RAF_User' ) || ! in_array( $email->id, $emails, true ) ) {
return;
}
$code = '';
if ( $order->get_user_id() ) {
$raf_user = new WPGens_RAF_User( $order->get_user_id() );
$code = $raf_user->get_referral_id();
} elseif ( get_option( 'gens_raf_allow_guests' ) ) {
$db = new WPGens_RAF_DB();
$guest = $db->get_guest_referral_by_email( $order->get_billing_email() );
$code = $guest ? $guest['referral_code'] : '';
}
// Skip customers without a code, and the "no orders" or "banned" messages.
if ( ! $code || strpos( $code, 'ref' ) !== 0 ) {
return;
}
$link = add_query_arg( 'raf', $code, trailingslashit( WPGens_RAF_User::get_referral_landing_url() ) );
if ( $plain_text ) {
echo "\nRefer a friend and earn a reward. Your referral link: " . esc_url_raw( $link ) . "\n";
return;
}
echo '<p style="text-align:center;margin:24px 0;">Refer a friend and earn a reward. Your referral link:<br><a href="' . esc_url( $link ) . '">' . esc_html( $link ) . '</a></p>';
}, 30, 4 );
Guest customers get a link only if Enable referral links for guests is on and they already have a guest referral code. For an automatic invitation that also covers new guests, use the Reminder email.
Show the advocate in the admin New order email
This adds “Referral order” with the advocate’s name and email, and the fraud check result, to emails sent to the shop.
add_action( 'woocommerce_email_order_meta', function ( $order, $sent_to_admin, $plain_text ) {
if ( ! $sent_to_admin ) {
return;
}
$code = $order->get_meta( '_wpgens_raf_id' ) ?: $order->get_meta( '_raf_id' );
if ( ! $code ) {
return;
}
$advocate = $code;
$user_ids = get_users( array(
'meta_key' => 'gens_referral_id',
'meta_value' => $code,
'number' => 1,
'fields' => 'ID',
) );
if ( $user_ids ) {
$user = get_userdata( $user_ids[0] );
$advocate = $user->display_name . ' (' . $user->user_email . ')';
} elseif ( class_exists( 'WPGens_RAF_DB' ) ) {
$db = new WPGens_RAF_DB();
$guest = $db->get_guest_referral_by_code( $code );
if ( $guest ) {
$advocate = $guest['name'] . ' (' . $guest['email'] . ', guest)';
}
}
$meta = $order->get_meta( '_wpgens_raf_meta' );
$info = is_array( $meta ) && ! empty( $meta['info'] ) ? $meta['info'] : '';
if ( $plain_text ) {
echo "\nReferral order. Referred by: " . $advocate . "\n" . ( $info ? $info . "\n" : '' );
return;
}
echo '<h3>Referral order</h3><p>Referred by: ' . esc_html( $advocate ) . '</p>';
if ( $info ) {
echo '<p>' . esc_html( $info ) . '</p>';
}
}, 20, 3 );
Orders without a referral are unchanged. The same information is always in the order’s Refer a Friend Data box.
Check it worked
- Place a test order through a referral link with a new customer.
- The New order email to the shop should show “Referral order” and the advocate.
- Mark the order Completed. The customer’s Completed order email should show their own referral link, if they have an account.
You can also preview emails with an email preview plugin, or under WooCommerce → Settings → Emails if your WooCommerce version has the email preview.