John Davidson

php - WooCommerce WPGens Referral in every email

0 comments
Message:


I use a Referral plugin for WooCommerce developed by WPGens (I already submitted a ticket but I am not premium so I don't have many hopes


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 );

But this add the information to the bottom of the WooCommerce Mails is there a way to show the same information but above the Order Details? In terms of marketing it would be first thing customers see.



My Answer:
Here is an example code snippet that you can use to add the referral link in every email sent to customers:

php
add_action( 'woocommerce_email_header', 'add_referral_link_to_email', 10, 2 );

function add_referral_link_to_email( $email_heading, $email ) {
if ( $email instanceof WC_Email ) {
$order_id = $email->object->get_id();
$referral_link = get_referral_link( $order_id );

if ( $referral_link ) {
echo '

Get a discount on your next purchase by referring a friend: Refer a friend

';
}
}
}

function get_referral_link( $order_id ) {
// Get the user ID from the order
$user_id = get_post_meta( $order_id, '_customer_user', true );

// Get the referral link for the user
$referral_link = gens_raf_get_referral_link( $user_id );

return $referral_link;
}


This code will add a referral link in the header of every email sent to customers. You can customize the HTML markup and styling as needed. Make sure to replace `gens_raf_get_referral_link()` with the actual function provided by the WPGens Referral plugin.

Rate this post

5 of 5 based on 7670 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us