简体   繁体   中英

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?

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM