• Resolved chilidogs

    (@chilidogs)


    Hi Ewout!
    First of all, congratulations for your great plugin, a gret work!

    I am facing, what I guess is an easy issue. I am not being able to get a string from the function: $wpo_wcpdf->get_custom_field( ‘inscription_textbox2’)

    My code is:

    function mycustom_headers_filter_function( $headers, $email ) {
    global $wpo_wcpdf;

    $email_address_to_send = $wpo_wcpdf->get_custom_field( ‘inscription_textbox2’);
    $headers .= ‘BCC: ‘ . $email_address_to_send . “\r\n”;
    return $headers;
    }

    The problem is that apparently the email string that comes from the textbox called ‘inscription_textbox2’ is not being concatenated in the headers variable …

    I am sure that the email is being filled in the ‘inscription_textbox2’ text field since it is being correctly showed in the pdf.

    Do you have any idea about how to solve it?

    Thanks a lot in advance!
    Angel.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hello Angel,
    The problem is that at the point where the headers filter is executed, the $order is not known yet to the PDF invoice plugin because the attachment is created later.
    However, the latest version of WooCommerce has a third argument to the woocommerce_email_headers filter, which holds the $order object (or $user object for user emails, etc.). Note the ‘3’ in the add_filter arguments.

    
    add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
    function mycustom_headers_filter_function( $headers, $email_id, $order ) {
        if ($email == 'customer_completed_order') {
            $email_address_to_send = get_post_meta( $order->id, 'inscription_textbox2', true );
            $headers .= 'BCC: ' . $email_address_to_send . "\r\n";
        }
     
        return $headers;
    }
    

    Important: you cannot globally apply this because for some emails (out of stock, user emails, etc.) there is not $order object available and this can throw errors.

    Hope that helps!
    Ewout

    Thread Starter chilidogs

    (@chilidogs)

    Hi Ewout!
    Thanks a lot for your quick answer!
    Actually this solution was intended only for sending the e-mail with the attached invoice to the correct e-mail address, so I guess that with the particularization you stated with $email == ‘customer_completed_order’ it should be OK to affect only this invoice attached mail, Am I right?

    I will test it this evening!
    Thanks again!

    Plugin Contributor Ewout

    (@pomegranate)

    Hi Chilidogs,
    That’s correct, only I made a small error in the code, $email should be $email_id:

    
    add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
    function mycustom_headers_filter_function( $headers, $email_id, $order ) {
        if ($email_id == 'customer_completed_order') {
            $email_address_to_send = get_post_meta( $order->id, 'inscription_textbox2', true );
            $headers .= 'BCC: ' . $email_address_to_send . "\r\n";
        }
     
        return $headers;
    }
    
    Thread Starter chilidogs

    (@chilidogs)

    Hi Ewout!

    thanks a lot, the second version worked!

    now What I am trying to do is to send the e-mail ONLY to the address in $email_address_to_send variable … I am trying to empty the variable $headers and afterwards to fill it with the $email_address_to_send but, I am still receiving the e-mail in the e-mail address of the user … I guess I am losing some details here … Any hint about it? Thanks a lot for your expert help!

    Thread Starter chilidogs

    (@chilidogs)

    Hi again Ewout!

    For this second approach of changing the recipient of the mail it finally worked using the ‘woocommerce_email_recipient_’ filter as follows:

    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
    
    function your_email_recipient_filter_function($recipient, $object) {
            $email_address_to_send = get_post_meta( $object->id, 'inscription_textbox2', true );
    		if ( null == $email_address_to_send ){ 
    	       return $recipient;
            }
            else{ 
                 $recipient = null;
    	     $recipient =  $email_address_to_send ;
                 return $recipient;
    	    }
    }

    Thanks a lot for your time and effort replying so quickly!
    Best regards!
    Angl.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘PHP Concatenation not working with’ is closed to new replies.