phpwordpresswoocommerceemail-notificationspayment-method

Add custom message according to payment method in WooCommerce customer processing order email notification


I'm adding a text with a hook for the email, but I'd like it to change according to the payment method.

My code attempt:

add_action( 'woocommerce_email_before_order_table', 'bbloomer_add_content_specific_email', 20, 4 );
  
function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email, $order_id ) {
    $order = wc_get_order( $order_id );
       $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;
   if ( $email->id == 'customer_processing_order' ) {
       if ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) {
           echo '<p>One text</p>';
    }elseif ( get_post_meta($order->id, '_payment_method', true) == 'wocommerce_yape_peru' ) {
           echo '<p>Another text</p>';
    }
       elseif ( get_post_meta($order->id, '_payment_method', true) == 'woo-mercado-pago-custom' ) {
           echo '<p>Third text</p>';
    }
   }
}

Unfortunately this does not have the desired result. Am I doing something wrong? any advice?


Solution

  • You can use $order->get_payment_method(), so you get:

    function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {   
        // Only for order processing email 
        if ( $email->id == 'customer_processing_order' ) {
            // Get payment method
            $payment_method = $order->get_payment_method();
            
            // Compare
            if ( $payment_method == 'cod' ) {
                echo 'text 1';
            } elseif( $payment_method == 'bacs' ) {
                echo 'text 2';
            } else {
                echo 'text 3';
            }
        }    
    }
    add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );