WooCommerce emails not being triggered by this code

Viewed 321

I have created these email templates with WooCommerce booster:

enter image description here

and am trying to manually send them off using this code. Everything works except the Custom_6 and Custom_7 emails.

<?php
    add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
    
    function woocommerce_thankyou_change_order_status( $order_id ){
        
        if( ! $order_id ) return;

        $order = new WC_Order($order_id);
        $payment_gateway = $order->get_payment_method();

        if ($payment_gateway == 'invoice') {
                
            $order->update_status('wc-pending-inv', 'order_note'); 

            //sending emails
            //customer
            WC()->mailer()->get_emails()['WC_Email_WCJ_Custom_6']->trigger( $order_id );
            //admin
            WC()->mailer()->get_emails()['WC_Email_WCJ_Custom_7']->trigger( $order_id );
            WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']->trigger( $order_id );

        }
    }

?>

Unfortunately, the WC_Email_WJC_Custom_6 and 7 do not send emails, but the WC_Email_Customer_Processing_Order does. Am I missing anything here? Perhaps the trigger shouldn't be order id?

Cheers!

1 Answers

If mail not working you should follow a few steps:

  1. Before check WordPress mail you should Check mail working on your server or not? because if your server does not respond you can't send mail any how. So your test the server first like this(very simple way):

    a) Create a new page on your root folder(WordPress folder) like test.php

    b) Open link https://www.w3schools.com/php/func_mail_mail.asp and copy mail code and past on test.php and change mail and run, If you got mail means there is no any server issue.

    c) If you did not get mail, means it's your server issue. so you should be fixed first or you can send mail to your server support regarding this "Mail not working on my server"

But ff you got a mail from test.php means server is working then you can move on your woocommerce thankyou page mail:

Case 1: download any SMPT plugin like this https://wordpress.org/plugins/easy-wp-smtp/ and set up then again place the order and check the mail.

Case 2: If you not getting mail then update thankyou page functionality by using function.php:

 add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
 function woocommerce_thankyou_change_order_status( $order_id ){
    $order = wc_get_order($order_id);
    $wc_emails = WC()->mailer()->get_emails();
    if( empty( $wc_emails ) ) return;

    if ($order->has_status( 'on-hold' ))
        $email_id = 'customer_on_hold_order';
    elseif ($order->has_status( 'processing' ))
        $email_id = 'customer_processing_order';
    elseif ($order->has_status( 'completed' ))
        $email_id = 'customer_completed_order';
    else
        $email_id = "pending";

    foreach ( $wc_emails as $wc_mail )
        if ( $wc_mail->id == $email_id )
            $wc_mail->trigger( $order->get_id() );
}
Related