WooCommerce: Show notice on new order email if spe

2019-08-10 16:15发布

问题:

I want to add a notice to "new order" email sent to admins before the table so if customer paid with "Purchase Order", processing team will know the payment is pending.

Research & Work Done: I spent some time researching various tuts and docs and came up with the Version 1 code but it's not showing anything on the order email (I changed the code Version 2 and tried again but to no avail). Are the codes wrong? I just want to confirm before I look into other options. Thank you

Version 1

/* Payment pending instruction if payment method is "Purchase Order" */
function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $sent_to_admin ) {
        echo '<p><strong>Payment Method:</strong> '.$order->payment_method_title().'</p>';
        if ( 'Purchase Order' == $order->get_payment_method_title() ) {
            /* if purchase order method is used */
            echo '<p><strong>Note:</strong> Payment is pending, please contact customer for payment before processing order for shipping.</p>';
        }
    }
}
add_action( 'woocommerce_before_email_order_table', 'add_order_instruction_email', 20, 4 );

Version 2

/* Payment pending instruction if payment method is "Purchase Order" */
function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'new_order' ) {
        echo '<p><strong>Payment Method:</strong> '.$order->payment_method_title().'</p>';
        if ( 'Purchase Order' == $order->get_payment_method_title() ) {
            /* if purchase order method is used */
            echo '<p><strong>Note:</strong> Payment is pending, please contact customer for payment before processing order for shipping.</p>';
        }
    }
}
add_action( 'woocommerce_before_email_order_table', 'add_order_instruction_email', 10, 2 );

回答1:

If someone else is interested in this, here's what worked for me:

/* Payment pending instruction if payment method is "Purchase Order" */
function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id == 'new_order' ) {
        if ( 'Purchase Order' == $order->get_payment_method_title() ) {
            echo '<p><strong>Note:</strong> Payment is pending, please contact customer for payment before processing order for shipping.</p>';
        }
    }
}
add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 10, 4 );