Woocommerce - How to send custom emails based on payment type

前端 未结 2 1574
再見小時候
再見小時候 2021-01-05 17:26

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

  • Check Payment
  • Western Union
  • Cash On Delivery

I

2条回答
  •  没有蜡笔的小新
    2021-01-05 18:04

    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 ' . "\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 '
    Method Title: "'.$payment_gateway->title .'" / Method slug: "'.$payment_gateway->id .'"
    '; } 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.

提交回复
热议问题