6

Here is the problem. My woocommerce website has 3 different payment options -

  • Check Payment
  • Western Union
  • Cash On Delivery

If my buyer checkout with "Check Payment" I want to send him an automated email that outlines the steps to make a check payment. If he checkout with "Western Union" I want to email him my Western Union information as an automated email. Another automated email should be send for Cash On Delivery.

Usually in Woocommerce you have a single email sent to customer for all completed orders, in my case I need 3 different emails depending on the payment option.

So I started using this tutorial to make a custom email - https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/

The tutorial above is used to make custom emails for expedited shipping. This is the line of code used for the same from the tutorial -

// bail if shipping method is not expedited
if ( ! in_array( $this->object->get_shipping_method(), array( 'Three Day Shipping', 'Next Day Shipping' ) ) )
    return;

What will the line of code be if I want to check what the payment method is? I want to check if the payment method is "Check Payment" so that I can send him a custom email.

Please let me know if you have any idea.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Adam
  • 657
  • 2
  • 9
  • 22

2 Answers2

7

You can send a different customized email for each payment method with this custom function using thank_you hook. There is many options that you can set, for this refer to wp_mail() function code reference.

Here is the code:

add_action( 'woocommerce_thankyou', 'wc_cheque_payment_method_email_notification', 10, 1 );
function wc_cheque_payment_method_email_notification( $order_id ) {
    if ( ! $order_id ) return;

    $order = wc_get_order( $order_id );

    $user_complete_name_and_email = $order->billing_first_name . ' ' . $order->billing_last_name . ' <' . $order->billing_email . '>';
    $to = $user_complete_name_and_email;

    // ==> Complete here with the Shop name and email <==
    $headers = 'From: Shop Name <name@email.com>' . "\r\n";

    // Sending a custom email when 'cheque' is the payment method.
    if ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) {
        $subject = 'your subject';
        $message = 'your message goes in here';
    }
    // Sending a custom email when 'Cash on delivery' is the payment method.
    elseif ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) {
        $subject = 'your subject';
        $message = 'your message goes in here';
    }
    // Sending a custom email when 'Western Union' is the payment method.
    else {
        $subject = 'your subject';
        $message = 'your message goes in here';
    }
    if( $subject & $message) {
        wp_mail($to, $subject, $message, $headers );
    }
}

This code goes in functions.php file of your active child theme (or theme) or also in any plugin file.

This is tested and it works.


— Update — Related to your comments.

Getting your available payment methods slugs (temporary, just to get all slugs). This will display your available payment methods slugs on shop page or in product pages too. After usage, just remove it.

Here is that functional code:

function the_available_payment_gateways(){
    foreach(WC()->payment_gateways->get_available_payment_gateways() as $payment_gateway)
        echo '<div style="border:solid 1px #999">Method Title: "'.$payment_gateway->title .'" / Method slug: "'.$payment_gateway->id .'"</div>';
}
add_action( 'woocommerce_before_main_content', 'the_available_payment_gateways', 1 );

This code goes in function.php file of your active child theme (or theme). Remove it after usage.

Adam
  • 657
  • 2
  • 9
  • 22
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hey Loic, thanks for the code. I am about to test it and then I will accept the answer. One question, I have more than 3 payment gateways so I cannot use "else" for Western Union. Do you know how I can find what is the get_post_meta for Western Union (like "cod" or "cheque") ? – Adam Oct 07 '16 at 16:34
  • @Adam **Updated my answer** with a function that will display on shop page and on products pages all available payment methods slugs… Once done, leave me a comment here with those slugs, and I will update my code. – LoicTheAztec Oct 08 '16 at 08:06
  • I will only test this code in 2 days but I will mark the answer because it looks like its correct. But thanks a lot Loic. If I have any doubts I will comment, but thanks again. – Adam Oct 10 '16 at 08:35
  • Hey Loic, I just tried your code in functions.php and its giving me an Error 500. Any idea why? I am still trying to resolve it. – Adam Oct 13 '16 at 09:46
  • Okay, its just because a curly bracket was missing in the last function. I edited it, its working now :) – Adam Oct 13 '16 at 09:54
0

1) At first, you should ask WP question at wordpress.stackexchange.com

2) In general, the easier way is to send an email, where is listed ALL options (divided with paragraphs, or even links), like this:

Hello....
.....
.....
===== Payment Methods =======
1) Western union - follow this instruction: http://example.com/how-to-1
2) Paid by check - follow this instruction: http://example.com/how-to-2
3) Cash on Delivery - follow this instruction: http://example.com/how-to-3
.....
.....

3) If you know a bit more,then here is listed all hooks - https://docs.woocommerce.com/wc-apidocs/hook-docs.html (search for word EMAIL) and then customize the desired hook.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 1
    Sorry to inform you that all related questions around WooCommerce are off topic in wordpress stackexchange. So please update your answer. Thanks – LoicTheAztec Oct 06 '16 at 07:47
  • @tazo I cannot send an email listing all options due to several reasons. I have to sen individual emails for the three different payment options. – Adam Oct 06 '16 at 08:35