简体   繁体   English

显示自定义动态字段存储数据 woocommerce 订单管理和来自元的感谢页面。?

[英]Display custom dynamic fields stored data woocommerce order admin and thankyou page from meta.?

I am stuck from past few days with to store data of dynamically generated fields on my woocommerce checkout page.过去几天我一直在我的 woocommerce 结帐页面上存储动态生成的字段的数据。 I am using woocommerce booking plugin in which i have to get names of person type.我正在使用 woocommerce 预订插件,我必须在其中获取人员类型的姓名。 for that i have did this.为此,我已经这样做了。

// 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"));
    }
}
}

I am getting fields on my checkout page those working perfectly but now i want them to stored on database and display on admin order page and thankyou page of order, which is become deperession to me how can i get to resolve this, Stackoverflow is only platform i believe can get solution.我在我的结帐页面上找到了那些工作正常的字段,但现在我希望它们存储在数据库中并显示在管理订单页面和感谢订单页面上,这对我来说已经成为 deperession 我该如何解决这个问题,Stackoverflow 只是平台我相信可以得到解决。

Edit After spending alot of hours i have been successfull to store custom data into database, now i want it to be display on admin and thankyou page.编辑花费大量时间后,我已成功将自定义数据存储到数据库中,现在我希望它显示在管理和感谢页面上。

Here is my code which saving fields to post 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.''] ) );
        }
    }
}
}
// 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++;
}

 }

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

相关问题 在帐单地址后显示 WooCommerce 管理订单中的自定义元数据 - Display custom meta data in WooCommerce admin order after billing address 在 WooCommerce admin 订单列表自定义列上显示订单商品元数据 - Display order item meta data on WooCommerce admin order list custom column 在 WooCommerce 订单管理列表上显示带有自定义用户元数据的自定义列 - Display custom column with custom user meta on WooCommerce order admin list 在Woocommerce中的管理订单编辑页面上获取嵌套的自定义字段元数据 - Get nested custom fields meta data on admin order edit pages in Woocommerce 在Woocommerce管理员订单预览上显示自定义数据 - Display custom data on Woocommerce admin order preview 在 WooCommerce 感谢页面上显示自定义复选框字段 state - Display custom checkbox field state on WooCommerce thankyou page 如何从 Woocommerce 订单管理员保存和显示用户元数据 - How to save and display user meta from Woocommerce order admin 在 WooCommerce 订单列表中显示 Dokan 自定义订单元数据 - Display Dokan custom order meta data in WooCommerce order list 将 WooCommerce 订单项自定义字段总和保存为新的元数据 - Save WooCommerce order item custom fields sum as a new meta data 将多个 WooCommerce 自定义结帐字段保存为订单元数据 - Save multiple WooCommerce custom checkout fields as order meta data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM