1
votes

I'm trying to add order_comments field from woocommerce checkout page to new order admin email.

Here's how i did with other fields (it's for conditional shipping method):

add_action ('woocommerce_email_customer_details', 'custom_email_customer_details', 15, 4);
function custom_email_customer_details( $order, $sent_to_admin, $plain_text, $email ){

    // Only for "New Order" email notification
    if ( 'new_order' != $email->id ) return;

    // Only "Flat Rate" Shipping Method
    if ( $order->has_shipping_method('flat_rate') ) {
        $order_id = $order->get_id(); // The Order ID

        // Test output
        echo '<table>';
        echo '<tr>';
        echo '<td>';
                 echo '<br><strong>'.__('Имя').':</strong> ' . get_post_meta( $order->id, '_billing_first_name', true ); 
        echo '<br><strong>'.__('Фамилия').':</strong> ' . get_post_meta( $order->id, '_billing_last_name', true );
                     echo '<br><strong>'.__('Метод доставки: Доставка курьером по Киеву').'</strong> '; 
         echo '<br><strong>'.__('Улица').':</strong> ' . get_post_meta( $order->id, '_shipping_address_1', true ); 
        echo '<br><strong>'.__('Дом').':</strong> ' . get_post_meta( $order->id, '_shipping_address_2', true );
        echo '<br><strong>'.__('Подъезд').':</strong> ' . get_post_meta( $order->id, '_shipping_city', true ) ;
        echo '<br><strong>'.__('Этаж').':</strong> ' . get_post_meta( $order->id, '_shipping_state', true ) ; 
        echo '<br><strong>'.__('Квартира').':</strong> ' . get_post_meta( $order->id, '_shipping_postcode', true ) ;
        echo '<br><strong>'.__('Время доставки').':</strong> ' . get_post_meta( $order->id, 'Время доставки', true ) ;
        echo '<br><strong>'.__('Комментарий покупателя').':</strong> ' .    ( $order->id, 'order_comments', true ) ;
        echo '</td>';
        echo '</tr>';
        echo '</table>';
                $mailer = WC()->mailer();

    remove_action( 'woocommerce_email_customer_details', array( $mailer, 'email_addresses' ), 20, 4 );

    }

Adding next line doesn't help:

echo '<br><strong>'.__('Order notes').':</strong> ' .   ( $order->id, '_order_comments', true ) ;

So what am I doing wrong? Thanks in advance

1
people will not spend time trying to figure out what is going on. what do you get? What is the exact problem? what do you expect?Bojan Petkovic

1 Answers

1
votes

If you are trying to get the Order customer note, you should need to use the following WC_Order method:

echo '<br><strong>'.__('Order notes').':</strong> '.$order->get_customer_note();

Also $order->id is outdated and replaced by $order->get_id() in WooCommerce 3+.

As you Get $order, the instance of the WC_Order object you can use on it all WC_Order methods in your code instead:

add_action ('woocommerce_email_customer_details', 'custom_email_customer_details', 15, 4);
function custom_email_customer_details( $order, $sent_to_admin, $plain_text, $email ){

    // Only for "New Order" email notification
    if ( 'new_order' != $email->id ) return;

    // Only for "Flat Rate" Shipping Method
    if ( ! $order->has_shipping_method('flat_rate') ) return;

    // Test output
    echo '<table><tr>
        <td><br>
            <strong>'.__('Имя').':</strong> '.$order->get_billing_first_name().'<br>
            <strong>'.__('Фамилия').':</strong> '.$order->get_billing_last_name().'<br>
            <strong>'.__('Метод доставки: Доставка курьером по Киеву').'</strong> <br>
            <strong>'.__('Улица').':</strong> '.$order->get_shipping_address_1().'<br>
            <strong>'.__('Дом').':</strong> '.$order->get_shipping_address_2().'<br>
            <strong>'.__('Подъезд').':</strong> '.$order->get_shipping_city().'<br>
            <strong>'.__('Этаж').':</strong> '.$order->get_shipping_state().'<br>
            <strong>'.__('Квартира').':</strong> ' . $order->get_shipping_postcode().'<br>
            <strong>'.__('Время доставки').':</strong> ' . get_post_meta( $order->get_id(), 'Время доставки', true ).'<br>
            <strong>'.__('Комментарий покупателя').':</strong> ' . $order->get_customer_note().'
        </td>
    </tr></table>';

    $mailer = WC()->mailer();

    remove_action( 'woocommerce_email_customer_details', array( $mailer, 'email_addresses' ), 20, 4 );
}