douqing5981 2017-10-19 15:24
浏览 65
已采纳

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.

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>';
}
  • 写回答

1条回答 默认 最新

  • dqf35839 2017-10-19 17:07
    关注

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

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested on WooCommerce 3 and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现