简体   繁体   English

Woocommerce 在管理订单详细信息上显示自定义字段数据

[英]Woocommerce display custom field data on admin order details

I am using a custom checkout field to give my customers a 'Ship to a business address' option on the checkout page of my woocommerce store.我正在使用自定义结帐字段在我的 woocommerce 商店的结帐页面上为我的客户提供“运送到公司地址”选项。 Most of the code is working properly, but I am unable to display whether or not they checked the box in the admin order details in the back end.大多数代码工作正常,但我无法显示他们是否选中了后端管理订单详细信息中的框。

I have added a custom checkout field to my woocommerce shop, and saved the data to the order meta:我在 woocommerce 商店中添加了一个自定义结帐字段,并将数据保存到订单元数据中:

//add custom checkout field
add_filter( 'woocommerce_after_checkout_billing_form', 'gon_business_address_checkbox_field' );

function gon_business_address_checkbox_field( $checkout ){
    woocommerce_form_field( 'business_address_checkbox', array(
        'label'       => __('<h3 id="business_address_label">Check this box if you are shipping to a business.</h3>', 'woocommerce'),
        'required'    => false,
        'clear'       => false,
        'type'        => 'checkbox'
    ), $checkout->get_value( 'business_address_checkbox' ));
}


//update order meta
add_action('woocommerce_checkout_update_order_meta', 'gon_update_order_meta_business_address');

function gon_update_order_meta_business_address( $order_id ) {
    if ($_POST['business_address_checkbox']) update_post_meta( $order_id, 'Business Address?', 
    esc_attr($_POST['business_address_checkbox']));
}

Here's where I attempt to display this data on the admin order section.这是我尝试在管理订单部分显示此数据的地方。 I have followed the previous topics on this as closely as possible, but to no avail.我已尽可能密切关注前面的主题,但无济于事。

// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Ship to a Business Address', 'woocommerce').': </strong> ' . get_post_meta( $order->get_id(), '_business_address_checkbox', true ) . '</p>';
}

Is this issue possibly because I'm not using the checkbox in the correct way?这个问题是否可能是因为我没有以正确的方式使用复选框? The peculiar thing is that I am getting the info to print on the order emails as I wish by using this code:奇怪的是,我通过使用以下代码将信息打印在订单电子邮件上:

add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys' );
function my_custom_checkout_field_order_meta_keys( ) {
    if($_POST['business_address_checkbox']){
        $ship_to = 'YES';
    } else {
        $ship_to = 'NO';
    }
    echo '<h3>Ship to a business address? : '.$ship_to.'</h3>';
}

As you are saving this custom field data, using the meta_key : 'Business Address?'当您保存此自定义字段数据时,请使用meta_key'Business Address?' … So you need to use this meta_key to retrieve the data this way: ... 所以你需要使用这个meta_key来检索数据:

// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
    $business_address = get_post_meta( $order->get_id(), 'Business Address?', true );
    if( ! empty( $business_address ) )
        echo '<p><strong>'.__('Ship to a Business Address', 'woocommerce').': </strong> ' . $business_address . '</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

Tested on WooCommerce 3 and works.在 WooCommerce 3 上测试并有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Woocommerce管理订单详细信息 - 在订单详细信息页面上显示自定义数据 - Woocommerce Admin Order Details - Show custom data on order details page 在Woocommerce管理员订单预览上显示自定义数据 - Display custom data on Woocommerce admin order preview 在Woocommerce订单详细信息管理区域中显示自定义数据 - Show Custom Data in Woocommerce Order Details Admin Area 如何在woocommerce订单项目明细模板上显示自定义字段? - How to display custom field on woocommerce order item details template? 在 WooCommerce 订单管理页面 (ACF) 显示产品自定义字段 - Display product custom field in WooCommerce Order admin Page (ACF) 在Woocommerce管理员订单列表自定义列中显示结帐字段值 - Display checkout field value in Woocommerce admin order list custom column 在结帐中添加自定义字段并在 WooCommerce 管理订单页面中显示 - Add custom field in checkout and display it in WooCommerce admin order pages 在 WooCommerce 管理订单页面中保留并显示产品自定义字段值 - Keep and display product custom field value in WooCommerce Admin Order pages WooCommerce将自定义字段数据发布到“订单详细信息”页面上 - WooCommerce post custom field data to the Order Details page issue 自定义订单详细信息页面上的Woocommerce自定义字段 - Woocommerce Custom Field on Custom Order Details Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM