在WooCommerce订单和电子邮件通知中显示自定义字段值 [英] Display custom fields values in WooCommerce order and email notification

查看:61
本文介绍了在WooCommerce订单和电子邮件通知中显示自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于






前端订单页面电子邮件通知上随处显示自定义字段值的最好的简短方法是在订单总数表中显示它们,就像使用的付款方式一样:

  //显示所选的交付信息
add_filter('woocommerce_get_order_item_totals','chosen_delivery_item_order_totals' ,10,3);
函数selected_delivery_item_order_totals($ total_rows,$ order,$ tax_display){;
$ new_total_rows = [];

//遍历订单总行
foreach($ total_rows as $ key => $ total){
//获取选定的交货值
$ delivery_option = $ order-> get_meta('_ delivery_option');
$ delivery_datetime = $ order-> get_meta(’_ delivery_datetime');

//显示付款方式之前的交付信息
if(!empty($ delivery_option)&&'payment_method'=== $ key){
$ label =空($ delivery_datetime)? __('Delivery'):__('Delivery Date');
$ value =空($ delivery_datetime)吗? __( AZAP,$ domain):$ delivery_datetime;

//显示投放方式行
$ new_total_rows ['chosen_delivery'] = array('label'=> $ label,'value'=> $ value);
}
$ new_total_rows [$ key] = $ total;
}

return $ new_total_rows;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。








相关主题:选择WooCommerce交付方式后选择日期和时间


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.

解决方案

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

这篇关于在WooCommerce订单和电子邮件通知中显示自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆