Woocommerce - How to send personalized emails based on payment type

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

  • Check payment.
  • Western Union
  • C.O.D

If my check is made by the buyer using "Check payment", I want to send him an automatic email that describes the steps to pay for the check. If he checks with Western Union, I want to send him his email as an automated email. Another automatic email must be sent for cash on delivery.

Usually in Woocommerce you have one email sent to the client for all completed orders, in my case I need 3 different letters depending on the payment option.

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

The above tutorial uses special letters for expedited delivery. 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 be the line of code if I want to check which payment method? I want to check if the payment method is "Verify payment" so that I can send him a special email.

Please let me know if you have any ideas.

+5
source share
2 answers

You can send a different individual email address for each payment method using this custom function using hook_you hook. There are many options that you can set for this, referring to the link to the wp_mail () function .

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 the functions.php file of your active child theme (or theme), as well as in any plug-in file.

It is tested and works.


- Update - Related to your comments.

Getting available methods of paying for a bullet (temporary to get all slugs). This will display your available payment methods as a bullet on the store page or product pages. After use, simply remove it.

Here is the 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 is in the function.php file of your active child theme (or theme). Remove it after use.

+4
source

1) First you should ask a WP question at wordpress.stackexchange.com

2) In general, the easiest way is to send an email listing ALL options (divided into paragraphs or even links), for example:

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 little more, then all the hooks are listed here - https://docs.woocommerce.com/wc-apidocs/hook-docs.html (search for the word EMAIL), and then configure the desired hook.

+2
source

Source: https://habr.com/ru/post/1257775/


All Articles