Search code examples
phphtmlwordpresswoocommercewoocommerce-bookings

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. I am using woocommerce booking plugin in which i have to get names of person type. 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.

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.''] ) );
        }
    }
}
}

Solution

  • // 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++;
    }
    
     }