skip to Main Content

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


  1. You’re getting the error because you didn’t add the fourth $email argument available to the woocommerce_email_customer_details hook.

    The content will only be displayed in the Completed order and
    Processing order templates.

    Try this:

    add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 30, 4 );
    function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
    
        if ( $email->id == 'customer_completed_order' || $email->id == 'customer_processing_order' ) {
            $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>';
            }  
        }
        
    }
    

    The code has been tested and works.

    Login or Signup to reply.
  2. 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):

    add_action('woocommerce_email_customer_details', 'gens_raf_customer_email', 100, 4 );
    function gens_raf_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
        // Target specific notifications: Customer processing and completed orders
        if ( in_array( $email->id, ['customer_processing_order', 'customer_completed_order'] ) ) {
            $gens_raf = get_user_meta( $order->get_user_id(), "gens_referral_id", true );
            $code     = empty( $gens_raf ) ? $order->get_billing_email() : $gens_raf;
    
            if( $plain_text ){
                printf( __('Your referral code is: %s', 'gens-raf'), $code );
            } else {
                $output = sprintf( __("Your referral code is: %s", "gens-raf"), get_home_url() .'?raf='. $code );
                echo '<p style="text-align:center;margin-top:10px;">' . $output . '</p>';
            }  
        }  
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search