Woocommerce在管理员订单详细信息上显示自定义字段数据 [英] Woocommerce display custom field data on admin order details

查看:175
本文介绍了Woocommerce在管理员订单详细信息上显示自定义字段数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义结帐字段,在woocommerce商店的结帐页面上为客户提供运送至公司地址选项。大多数代码都可以正常工作,但是我无法显示他们是否在后端的管理订单详细信息中选中了该框。

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. 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.

我在woocommerce商店中添加了一个自定义结帐字段,并将数据保存到订单元:

I have added a custom checkout field to my woocommerce shop, and saved the data to the order meta:

//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>';
}


推荐答案

在保存此自定义项时字段数据,使用 meta_key 公司地址? …因此,您需要使用此 meta_key 这样检索数据:

As you are saving this custom field data, using the meta_key: 'Business Address?'… So you need to use this meta_key to retrieve the data this way:

// 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>';
}

代码包含在您活动的子主题的function.php文件中(

在WooCommerce 3上测试并可以正常工作。

Tested on WooCommerce 3 and works.

这篇关于Woocommerce在管理员订单详细信息上显示自定义字段数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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