Php 从meta显示自定义动态字段存储的数据,包括订单管理员和感谢页面。?

Php 从meta显示自定义动态字段存储的数据,包括订单管理员和感谢页面。?,php,html,wordpress,woocommerce,woocommerce-bookings,Php,Html,Wordpress,Woocommerce,Woocommerce Bookings,在过去的几天里,我一直忙于在我的woocommerce结帐页面上存储动态生成字段的数据。我正在使用woocommerce预订插件,其中我必须获得人名类型。 为此我做了这件事 // Add checkout custom text fields add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_text_fields', 20, 1 ); function add_checkout_custom_text_fiel

在过去的几天里,我一直忙于在我的woocommerce结帐页面上存储动态生成字段的数据。我正在使用woocommerce预订插件,其中我必须获得人名类型。 为此我做了这件事

// Add checkout custom text fields
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_text_fields', 20, 1 );
function add_checkout_custom_text_fields( $checkout) {
$index = 0;

// 1st Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item){

    // 2nd Loop through each unit related to item quantity
    for($i = 1; $i <= $cart_item['booking']['Adults']; $i++){
        $index++;

        woocommerce_form_field("my_field$index", array(
            'type' =>'text',
            'class'=>array('my-field-class form-row-wide'),
            'label'=>__('Adults')." ($i)",
            'placeholder'=>__('Enter adult name'),
            'required' => true,
        ), $checkout->get_value("my_field$index"));
    }

    for($i = 1; $i <= $cart_item['booking']['Childs']; $i++){
        $index++;

        woocommerce_form_field("my_field$index", array(
            'type' =>'text',
            'class'=>array('my-field-class form-row-wide'),
            'label'=>__('Childs')." ($i)",
            'placeholder'=>__('Enter child name'),
            'required' => true,
        ), $checkout->get_value("my_field$index"));
    }
}
}
//添加签出自定义文本字段
添加操作(“订单注释前的woocommerce”、“添加签出”和“自定义文本”字段”,20,1);
函数添加\签出\自定义\文本\字段($checkout){
$index=0;
//第一次循环购物车项目
foreach(WC()->cart->get_cart()作为$cart_项目){
//通过与物料数量相关的每个单元的第二个循环
对于($i=1;$i“文本”,
'class'=>array('my-field-class表单行宽'),
“标签”=>“($i)”,
“占位符”=>(输入成人姓名),
“必需”=>true,
),$checkout->get_value(“我的_字段$index”);
}
对于($i=1;$i“文本”,
'class'=>array('my-field-class表单行宽'),
'label'=>Childs'。“($i)”,
“占位符”=>(输入子名称),
“必需”=>true,
),$checkout->get_value(“我的_字段$index”);
}
}
}
我在我的结帐页面上得到的字段工作得很好,但现在我希望它们存储在数据库中,并显示在管理订单页面和感谢订单页面上,这取决于我如何解决这个问题,Stackoverflow是我认为唯一可以得到解决方案的平台

编辑后,花了很多小时,我已经成功地将自定义数据存储到数据库中,现在我希望它显示在管理和感谢页面

下面是我的代码,它保存了要发布meta的字段

// Save fields in order meta data
 add_action('woocommerce_checkout_create_order', 'save_custom_fields_to_order_meta_data', 20, 2 );
function save_custom_fields_to_order_meta_data( $order, $data ) {
$index = 0;

// 1st Loop through order items
foreach(WC()->cart->get_cart() as $cart_item){

    // 2nd Loop through each unit related to item quantity
    for($i = 1; $i <= $cart_item['booking']['Adults']; $i++){
        $index++;
        if (isset( $_POST['my_field'.$index.'']) && ! empty($_POST['my_field'.$index.'']) ){
            $order->update_meta_data( '_my_field_'.$index.'_'.$i, esc_attr( $_POST['my_field'.$index.''] ) );
        }
    }

    for($i = 1; $i <= $cart_item['booking']['Childs']; $i++){
        $index++;
        if (isset( $_POST['my_field'.$index.'']) && ! empty($_POST['my_field'.$index.'']) ){
            $order->update_meta_data( '_my_field_'.$index.'_'.$i, esc_attr( $_POST['my_field'.$index.''] ) );
        }
    }
}
}
//按顺序保存元数据字段
添加操作(“woocommerce\u checkout\u create\u order”、“save\u custom\u fields\u to\u order\u meta\u data”,20,2);
函数save_custom_fields_to_order_meta_data($order,$data){
$index=0;
//通过订单项目的第一个循环
foreach(WC()->cart->get_cart()作为$cart_项目){
//通过与物料数量相关的每个单元的第二个循环
对于($i=1;$i更新元数据(“'u我的字段”“$index.''''.$i,esc\u属性($'u POST['我的字段”“$index.'])));
}
}
对于($i=1;$i更新元数据(“'u我的字段”“$index.''''.$i,esc\u属性($'u POST['我的字段”“$index.'])));
}
}
}
}
//按编辑顺序显示字段
添加操作(“商业、管理、订单、账单地址后的数据”、“显示、管理、订单中的自定义字段”,20,1);
函数显示\自定义\字段\在\管理\订单中($order){
全球$wpdb;
$results=$wpdb->get_results(“从'wp_postETA'中选择*,其中'meta_key`LIKE'_成人%”和post_id=“.$order->get_id()”);
$i=1;
foreach($results作为$result){
回显“成人姓名“$i.”:“$result->meta_值”。

”; $i++; } $results=$wpdb->get_results(“从`wp_postETA`中选择*,其中`meta_key`LIKE'_child%'和post_id=“.$order->get_id()”); $i=1; foreach($results作为$result){ 回显“子名称“$i.”:。$result->meta_值。“

”; $i++; } }
// Display fields in order edit    

 add_action('woocommerce_admin_order_data_after_billing_address','display_custom_fields_in_admin_order', 20, 1);
  function display_custom_fields_in_admin_order( $order ){
  global $wpdb;
  $results = $wpdb->get_results( "SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '_adult%' and post_id = ".$order->get_id()."" );
 $i= 1;
 foreach($results as $result) {
 echo '<p><strong>Adult Name '.$i.':</strong>' . $result->meta_value . '</p>';
 $i++;
}

  $results = $wpdb->get_results( "SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '_child%' and post_id = ".$order->get_id()."" );
$i= 1;
foreach($results as $result) {
echo '<p><strong>Child Name '.$i.':</strong>' . $result->meta_value . '</p>';
$i++;
}

 }