3
votes

I am using wordpress 4.9.5 and woocommerce 3.3.5, I would like the following payment methods, order status and customer mails to be triggered.

  • Paypal = complete + payment confirmation email
  • BACS = on hold + order on hold email
  • COD = on hold + order on hold email

At this point everything works, except from not getting the "order on hold email" when using COD. The order status is been sat to "on hold", but the on-hold email is not beeing sendt!

This is the code I am using:

add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id )
        return;

    // Get an instance of the WC_Product object
    $order = wc_get_order( $order_id );

    // On hold status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
        $order->update_status( 'on-hold' );
    // Updated status to "completed" for paid Orders with all others payment methods
    } else {
        $order->update_status( 'completed' );
    }
}

function unhook_new_order_processing_emails( $email_class ) {
        // Turn off pending to processing for now
        remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );
        // Turn it back on but send the on-hold email
        add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_On_Hold_Order'], 'trigger' ) );
}
1

1 Answers

1
votes

First the hook is missing in your 2nd function… It should be:

add_action( 'woocommerce_email', 'unhook_new_order_processing_emails' );
function unhook_new_order_processing_emails( $email_class ) {
    // Turn off pending to processing for now
    remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );
    // Turn it back on but send the on-hold email
    add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_On_Hold_Order'], 'trigger' ) );
}

Official documentation: Unhook/remove WooCommerce Emails


Then to trigger "On hold" notification for "Cash on delivery" orders you can try the following:

// Trigger "On hold" notification for COD orders
add_action('woocommerce_order_status_on-hold', 'email_on_hold_notification_for_cod', 2, 20 );
function email_on_hold_notification_for_cod( $order_id, $order ) {
    if( $order->get_payment_method() == 'cod' )
        WC()->mailer()->get_emails()['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
}

Code goes in function.php file of your active child theme (or active theme)..