• Resolved kosy33

    (@kosy33)


    Hi Pinal,

    I am trying to send a copy of send-quote email to user_email address using woocommerce_email_headers filter but for some reason it does not work. I think $order variable cannot be found.
    My code works if $user_email and $user_name are given and set to a specfic value like [email protected], this code works as well if $email_id is woocommerce ‘processing_order’ email but not if it is ‘qwc_send_quote’.

    Here is my code

    function filter_woocommerce_email_headers( $header, $email_id, $order ) {
        // For a given email id & user id exists (no guests)
        if ( $email_id == 'qwc_send_quote' && $order->get_user_id() > 0 ) {
            // Get an instance of the WP_User object
            $user = $order->get_user();
            
            // Get user email
            $user_email = $user->user_email;
            
            // Get first & last name
            $user_name = $user->first_name . ' ';
            $user_name .= $user->last_name;
        
            // Prepare the the data
            $formatted_email = utf8_decode( $user_name . ' <' . $user_email . '>' );
    
            // Add Cc to headers
            $header .= 'Cc: ' . $formatted_email . '\r\n';
        }
    
        return $header;
    }
    add_filter( 'woocommerce_email_headers', 'filter_woocommerce_email_headers', 10, 3 );
    

    Could you please help me with that issue?
    Thank you
    Jean

    • This topic was modified 3 years, 2 months ago by kosy33.
    • This topic was modified 3 years, 2 months ago by kosy33.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author pinal.shah

    (@pinalshah)

    Hi Jean,

    I was able to replicate the issue of $order object not being found. However, the email id is being sent correctly.

    I’ve made a minor change in your code to get it working.

    function filter_woocommerce_email_headers( $header, $email_id, $order_id ) {
    	$order = new WC_Order( $order_id );
        // For a given email id & user id exists (no guests)
        if ( $email_id == 'qwc_send_quote' && $order->get_user_id() > 0 ) {
            // Get an instance of the WP_User object
            $user = $order->get_user();
            
            // Get user email
            $user_email = $user->user_email;
            
            // Get first & last name
            $user_name = $user->first_name . ' ';
            $user_name .= $user->last_name;
        
            // Prepare the the data
            $formatted_email = utf8_decode( $user_name . ' <' . $user_email . '>' );
    
            // Add Cc to headers
            $header .= 'Cc: ' . $formatted_email . '\r\n';
        }
    
        return $header;
    }
    add_filter( 'woocommerce_email_headers', 'filter_woocommerce_email_headers', 10, 3 );

    However, your code has a condition for user_id > 0. Please note that user_id > 0 only when the quote request comes through a registered user. The user ID is 0 for guest users.

    I hope the above helps. Please let me know if you have any further queries.

    Thanks,
    Pinal

    Thread Starter kosy33

    (@kosy33)

    Dear Pinal,
    Thank you for your prompt answer and your help, I copied and paste your code above and the problem stands except that quote is now sent to billing email address (it was not the case before with my code). User email address does not receive any mail yet.
    My code, as yours, works for $email_id == ‘processing_order’ when woocommerce email order notification are triggered, email is sent to both addresses. I cannot understand why this does not work with the send-quote template.

    • This reply was modified 3 years, 2 months ago by kosy33.
    • This reply was modified 3 years, 2 months ago by kosy33.
    Plugin Author pinal.shah

    (@pinalshah)

    That’s strange Jean.

    The only change I made in your code is adding the first line $order = new WC_Order( $order_id );.

    The code is being executed at my end for the send quote email ID.

    Is it possible for you to send me the admin & FTP access to your staging/dev site where you are checking this?

    Please send the details to pinalj1612 at gmail dot com.

    I’ll try to look into this later tonight.

    Thanks,
    Pinal

    Thread Starter kosy33

    (@kosy33)

    Thank you Pinal,
    I will send you the email with details as requested

    Best regards

    Plugin Author pinal.shah

    (@pinalshah)

    Thanks Jean.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘send-quote email send copy to user email address’ is closed to new replies.