Display custom fields values in WooCommerce order and email notification

丶灬走出姿态 提交于 2019-12-23 12:39:21

问题


Based on "Choosing a date and time after choosing the WooCommerce delivery method" answer code, that displays custom Pickup fields and delivery dates, the following code displays the delivery data of those fields on the order edit pages.

Here is my code:

// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_fields_order_meta', 10, 1 );
function my_custom_fields_order_meta($order){   

    $delivery_option = $order->get_meta('_delivery_option');

    if( $delivery_option == 'date' ) {
    $delivery_datetime = $order->get_meta('_delivery_datetime');

    echo '<p><strong>'.__('Delivery').':</strong> ' . get_post_meta( $order->id, '_delivery_option', true ) . '</p>';
    echo '<p><strong>'.__('Delivery Date').':</strong> ' . get_post_meta( $order->id, '_delivery_datetime', true ) . '</p>';
    }
}

Unfortunately, only the delivery date that the customer chooses is displayed correctly, and the options of the radio button "As Soon As Possible" are not shown.

Apparently, I'm doing something wrong.

I would like also to display these fields values on the Thank You page and in the email.

Any help is appreciated.


回答1:


To display the custom fields values in backend order edit pages (if they are saved in database for the order), use the following:

// View fields in Edit Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_fields_value_admin_order', 10, 1 );
function display_custom_fields_value_admin_order( $order ){
    // Display the delivery option
    if( $delivery_option =  $order->get_meta('_delivery_option') )
        echo '<p><strong>'.__('Delivery type').':</strong> ' . $delivery_option . '</p>';

    // Display the delivery date
    if( $delivery_datetime = $order->get_meta('_delivery_datetime') )
        echo '<p><strong>'.__('Delivery Date').':</strong> ' . $delivery_datetime . '</p>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


The best shorter clean way to display the custom field values everywhere on frontend order pages and on email notifications is to display them in order totals table, just like used payment methods:

// Display the chosen delivery information
add_filter( 'woocommerce_get_order_item_totals', 'chosen_delivery_item_order_totals', 10, 3 );
function chosen_delivery_item_order_totals( $total_rows, $order, $tax_display ) {;
    $new_total_rows = [];

    // Loop through Order total lines
    foreach($total_rows as $key => $total ){
        // Get the chosen delivery values
        $delivery_option  = $order->get_meta('_delivery_option');
        $delivery_datetime = $order->get_meta('_delivery_datetime');

        // Display delivery information before payment method
        if( ! empty($delivery_option) && 'payment_method' === $key ){
            $label  = empty($delivery_datetime) ? __('Delivery') : __('Delivery Date');
            $value  = empty($delivery_datetime) ? __('AZAP', $domain) : $delivery_datetime;

            // Display 'Delivery method' line
            $new_total_rows['chosen_delivery'] = array( 'label' => $label,'value' => $value );
        }
        $new_total_rows[$key] = $total;
    }

    return $new_total_rows;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


Related thread: Choosing a date and time after choosing the WooCommerce delivery method



来源:https://stackoverflow.com/questions/55115256/display-custom-fields-values-in-woocommerce-order-and-email-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!