WooCommerce Custom Emails based on Payment Gateway

One of the most important things for an online store to flourish is to create a sense of trustworthiness with the customers. There are a lot of ways to do so. One easy and simple way is to send emails to the buyers with details like the payment type (cash/ credit card/ cheque) and the store name.

Sending custom messages to your customers in the email can also help you with your sponsors. For example, you can send emails to the buyers who used their credit card and make them feel much more prioritized.

In this tutorial, adding this functionality will be covered. First of all, you will have to paste the code below to the Theme Functions section.

Just go to Dashboard > Appearance > Theme Editor > Theme Functions and add the following code to the end of the existing code and click Update File.

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 <[email protected]>' . "\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 );
    }
}

 

Secondly, you need to replace ‘Shop name < [email protected] >’ with the name and email address of your store. Next, you need to replace the line ‘Your message goes in here’ with some custom message of your choice.

And there you go, you are all set to send custom emails to your customers according to their payment method.

Let us know your thoughts in the comments section below.

 

Leave a Reply