awesomemotive / wp-mail-logging

:envelope: Logs each email sent by WordPress.
49 stars 26 forks source link

Email sent to wrong user when woo order failed #191

Closed yckelvin closed 4 months ago

yckelvin commented 4 months ago

Dear Sir/Madam,

I add the below script to custom email for the woo failed order

add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
    if ( $new_status == 'cancelled' || $new_status == 'failed' ){
        $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
        $customer_email = $order->get_billing_email(); // The customer email
    }

    if ( $new_status == 'cancelled' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_Cancelled_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_Cancelled_Order']->trigger( $order_id );
    } 
    elseif ( $new_status == 'failed' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_Failed_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_Failed_Order']->trigger( $order_id );
    } 
}

The email was correctly sent to both custom email and administrator email but it also sent to another customer email

Screenshot 2024-05-02 at 10 02 25 PM

All the above orders are failed orders, email starts with sunny... is the correct email related to order #74677 but email alanlo... is not correct.

Screenshot 2024-05-02 at 9 35 18 PM

The above screenshot is the email content and you should find the receiver's email and email address at footer are not the same.

Please advise how I can fix it.

capuderg commented 4 months ago

Hi @yckelvin, our WP Mail Logging email is only collecting the data that is passed to the wp_mail function, so I think the issue for your case is happening on the WooCommerce level, so I suggest reaching out to the WooCommerce support, so they can help you out with the correct filter usage for your use case.