将自定义结帐字段(用户输入)添加到订单数据和用户元数据

Posted

技术标签:

【中文标题】将自定义结帐字段(用户输入)添加到订单数据和用户元数据【英文标题】:Add Custom Checkout Field (user input) to Order Data and User Meta 【发布时间】:2022-01-02 22:25:00 【问题描述】: 我编写此代码是为了让用户可以在结帐字段中添加更多用户数据 (灵感来自 *** 中发布的代码,但我再也找不到源代码了)

functions.php 中的 PHP:

add_action('woocommerce_before_order_notes','checkout_sections');
function checkout_sections()
    echo '<div>';      
        echo '<h3>'. __( 'Add New Member', 'woocommerce' ).'</h3>';
        echo '<div class="row" id="readroot">';
        echo '<label for="fullname[]">Name and Surname</label>';
        echo '<div class="col-lg-2">';
        echo '<div class="md-form form-sm new_row">';
        echo '<input type="text" name="fullname[]" id="newfullname" placeholder="Name and Surname" class="form-control" required>';
        echo '</div>';
        echo '</div>';
        echo '<br>';
        echo '<label for="ALIMCode[]">A.L.IM. Membership Code</label>';
        echo '<div class="col-lg-2">';
        echo '<div class="md-form form-sm new_row">';
        echo '<input type="text" name="ALIMCode[]" id="newalimcode" placeholder="A.L.IM. Membership Code" class="form-control" required>';
        echo '</div>';
        echo '</div>';
        echo '<br>';
        echo '<div class="col-lg-2">';
        echo '<div class="md-form form-sm">';
        echo '<a id="moreFields" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
        echo '<br>';
        echo '<div class="row" id="writeroot"></div>';
        echo '<div class="btn btn-sm btn-danger waves-effect waves-light pull-right" onclick="this.parentNode.parentNode;"><br>';

这是克隆该部分的 javascript 代码。 JS:

var counter = 0;

//document.getElementById('moreFields').onclick = moreFields;

window.onload = loadEventListener();

function loadEventListener() 
  let addRowAnchorTags = document.getElementsByClassName('moreFields');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function moreFields() 
  counter++;
  var newFields = document.getElementById('readroot').cloneNode(true);
  newFields.id = '';
  newFields.style.display = 'row';
  var newField = newFields.childNodes;
  for (var i = 0; i < newField.length; i++) 
    var theName = newField[i].name
    if (theName)
      newField[i].name = theName + counter;
  
  var insertHere = document.getElementById('writeroot');
  insertHere.parentNode.insertBefore(newFields, insertHere);
  setTimeout(function() loadEventListener();)

//window.onload = moreFields;

这是结帐阶段的结果:

我想要完成的事情 当我看到订单(woocommerce-order-data)和用户元数据时,我想在我的后端添加这些数据,这是下图中的模型:

我现在正在做什么以将数据存储在用户元数据中 现在,当客人下订单时,functions.php 中的代码将创建一个新用户,并在用户元数据和订单数据中添加所有自定义字段。 这是我正在使用的代码:

// create account when buy
function action_woocommerce_thankyou( $order_id ) 
    // Determines whether the current visitor is a logged in user.
    if ( is_user_logged_in() ) return;
    
    // Get $order object
    $order = wc_get_order( $order_id );
    
    // Get the user email from the order
    $order_email = $order->billing_email;

    // Check if there are any users with the billing email as user or email
    $email = email_exists( $order_email );  
    $user = username_exists( $order_email );

    // If the UID is null, then it's a guest checkout (new user)
    if ( $user == false && $email == false ) 
        // Random password with 12 chars
        $random_password = wp_generate_password();
        
        // Firstname
        $first_name = $order->get_billing_first_name();
        
        // Lastname
        $last_name = $order->get_billing_last_name();

        // Role
        $role = 'customer';   //'customer'

        // Company Name
        $companys = $order->get_billing_company();

        // payment method (luca)
        $paymentuser = $order->get_payment_method_title();
        
        // Username (luca)
        $usernames = $first_name.$last_name.$companys; 
        
        // Create new user with email as username, newly created password and userrole          
        $user_id = wp_insert_user(
            array(
                'user_email' => $order_email,  //$order_mail
                'user_login' => $usernames,
                'user_pass'  => $random_password,
                'first_name' => $first_name,
                'last_name'  => $last_name,
                'role'       => $role,
            )
        );
        
        // Get all WooCommerce emails Objects from WC_Emails Object instance
        $emails = WC()->mailer()->get_emails();

        // Send WooCommerce "Customer New Account" email notification with the password
        $emails['WC_Email_Customer_New_Account']->trigger( $user_id, $random_password, true );

        // (Optional) WC guest customer identification
        //update_user_meta( $user_id, 'guest', 'yes' );

        //codice per recuperare url file upload

        //$codedocu = $order->review_order_before_submit_upload_distinta_di_pagamento;
        //$urldocu =  'https://alimdigital.org/?checkout_fields_get=';
        //$urldocufine = '&checkout_fields_nonce=318982cbce';
        //$codefinal = $urldocu.$codedocu.$urldocufine;


        //$codedocu2 = $order->review_order_before_submit_upload_documento_identita;
        //$codedocu3 = $order->review_order_before_submit_upload_selfie_con_documento_identita;
        //$codedocu4 = $order->review_order_before_submit_upload_visura_camerale;
        //$codedocu5 = $order->review_order_before_submit_upload_proposta_di_statuto;
        
        //$codefinales = $urldocu.$codedocu2.$urldocufine; 
        //$codeselfie = $urldocu.$codedocu3.$urldocufine;
        //$codevisura = $urldocu.$codedocu4.$urldocufine;
        //$codestatuto = $urldocu.$codedocu5.$urldocufine;

        // User's billing data
        update_user_meta( $user_id, 'billing_tipologia_iscritto', $order->billing_tipologia_iscritto );
        update_user_meta( $user_id, 'billing_nazione_sede_alim_di_riferimento', $order->billing_nazione_sede_alim_di_riferimento );
        update_user_meta( $user_id, 'billing_sede_alim_di_riferimento', $order->billing_sede_alim_di_riferimento );
        update_user_meta( $user_id, 'billing_categoria_socio', $order->billing_categoria_socio);
        update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 );
        update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2);
        update_user_meta( $user_id, 'billing_city', $order->billing_city );
        update_user_meta( $user_id, 'billing_company', $order->billing_company );
        update_user_meta( $user_id, 'billing_codice_ateco_codice_nace', $order->billing_codice_ateco_codice_nace );
        update_user_meta( $user_id, 'billing_country', $order->billing_country );
        update_user_meta( $user_id, 'billing_email', $order->billing_email );
        update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name );
        update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name );
        update_user_meta( $user_id, 'billing_data_di_nascita', $order->billing_data_di_nascita );
        update_user_meta( $user_id, 'billing_phone', $order->billing_phone );
        update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode );
        update_user_meta( $user_id, 'billing_state', $order->billing_state );
        update_user_meta( $user_id, 'billing_tipologia_territoriale_di_sede_alim_richiesta', $order->billing_tipologia_territoriale_di_sede_alim_richiesta );
        
        update_user_meta( $user_id, 'billing_nazione', $order->billing_nazione);
        update_user_meta( $user_id, 'billing_provincia', $order->billing_provincia);
        update_user_meta( $user_id, 'billing_citta', $order->billing_citta);
        update_user_meta( $user_id, 'billing_indirizzo', $order->billing_indirizzo);
        update_user_meta( $user_id, 'billing_cap', $order->billing_cap);

        update_user_meta( $user_id, 'billing_nazione_di_rilascio', $order->billing_nazione_di_rilascio);
        update_user_meta( $user_id, 'billing_ente_di_rilascio', $order->billing_ente_di_rilascio);
        update_user_meta( $user_id, 'billing_numero_documento', $order->billing_numero_documento);
        update_user_meta( $user_id, 'billing_data_rilascio_documento', $order->billing_data_rilascio_documento );
        update_user_meta( $user_id, 'billing_data_scadenza_documento', $order->billing_data_scadenza_documento );

        update_user_meta( $user_id, 'billing_nome_cognome_presidente', $order->billing_nome_cognome_presidente );
        update_user_meta( $user_id, 'billing_codice_iscrizione_alim', $order->billing_codice_iscrizione_alim );

        update_user_meta( $user_id, 'billing_nome_cognome_vicepresidente', $order->billing_nome_cognome_vicepresidente );
        update_user_meta( $user_id, 'billing_codice_iscrizione_alim2', $order->billing_codice_iscrizione_alim2 );

        update_user_meta( $user_id, 'billing_nome_cognome_segretario', $order->billing_nome_cognome_segretario );
        update_user_meta( $user_id, 'billing_codice_iscrizione_alim22', $order->billing_codice_iscrizione_alim22 );

        update_user_meta( $user_id, 'billing_nome_cognome_tesoriere', $order->billing_nome_cognome_tesoriere );
        update_user_meta( $user_id, 'billing_codice_iscrizione_alim222', $order->billing_codice_iscrizione_alim22 );

        update_user_meta( $user_id, 'billing_nome_cognome_consigliere', $order->billing_nome_cognome_consigliere );
        update_user_meta( $user_id, 'billing_codice_iscrizione_alim2222', $order->billing_codice_iscrizione_alim2222 );

        

        // Link past orders to this newly created customer
        wc_update_new_customer_past_orders( $user_id );
        
        // Auto login
        wp_set_current_user( $user_id );
        wp_set_auth_cookie( $user_id );
      

add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 ); 

function filter_woocommerce_thankyou_order_received_text( $str, $order ) 
    // Determines whether the current visitor is a logged in user.
    if ( is_user_logged_in() ) return;
    
    // Get the user email from the order
    $order_email = $order->billing_email;
    
    // Check if there are any users with the billing email as user or email
    $email = email_exists( $order_email );  
    $user = username_exists( $order_email );

    // If the UID is null, then it's a guest checkout (new user)
    if ( $user == false && $email == false ) 
        // Link
        $link = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );

        // Format
        $format_link = '<a href="' . $link . '">logged in</a>';

        // Append to orginal string
        $str .= sprintf( __( ' An account has been automatically created for you and you are now %s. You will receive an email about this.', 'woocommerce' ), $format_link ); 
           

    return $str;

add_filter( 'woocommerce_thankyou_order_received_text', 'filter_woocommerce_thankyou_order_received_text', 10, 2 );

如果我在提供信息时犯了错误,请告诉我!提前致谢

【问题讨论】:

【参考方案1】:

我解决了这个问题! 这就是我所做的

我使所有我需要的部分不可见,并使用 Javascript 使它们通过单击显示。完成的每个字段都保存在订单数据中。 这是JS(第一个sn-p)和php(第二个sn-p)代码:

window.onload = loadEventListener();

function loadEventListener() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener2() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction2');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener3() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction3');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener4() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction4');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener5() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction5');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener6() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction6');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener7() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction7');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener8() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction8');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener9() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction9');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function loadEventListener10() 
  let addRowAnchorTags = document.getElementsByClassName('myFunction10');

  for(i=0;i<addRowAnchorTags.length; i++) 
      addRowAnchorTags[i].onclick = moreFields;
  


function myFunction() 
  var x = document.getElementById("newmember");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction2() 
  var x = document.getElementById("newmember2");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction3() 
  var x = document.getElementById("newmember3");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction4() 
  var x = document.getElementById("newmember4");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction5() 
  var x = document.getElementById("newmember5");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction6() 
  var x = document.getElementById("newmember6");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction7() 
  var x = document.getElementById("newmember7");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction8() 
  var x = document.getElementById("newmember8");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction9() 
  var x = document.getElementById("newmember9");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif


function myFunction10() 
  var x = document.getElementById("newmember10");
  if (x.style.display === "none") 
    x.style.display = "block";
   endif
//

add_action('woocommerce_after_order_notes', 'customise_checkout_field', 20, 1 );

function customise_checkout_field( $checkout )
    $domain = 'woocommerce';

    echo '<a onclick="myFunction()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<div id="newmember" style="display:none">  
    <h3>' . __( 'New Member', $domain ) . '</h3>';

    woocommerce_form_field( '01_new_role', array(
        'type'          => 'text',
        'class'         => array( '01_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('01_new_role') );

    woocommerce_form_field( '01_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '01_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('01_new_fullname') );

    woocommerce_form_field( '01_new_code', array(
        'type'          => 'text',
        'class'         => array( '01_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('01_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction2()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>';
    echo '</div>';
    echo '</div>'; // div newmember1 finish

    echo '<div id="newmember2" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '02_new_role', array(
        'type'          => 'text',
        'class'         => array( '02_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('02_new_role') );

    woocommerce_form_field( '02_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '02_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('02_new_fullname') );

    woocommerce_form_field( '02_new_code', array(
        'type'          => 'text',
        'class'         => array( '02_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('02_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction3()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>'; // div newmember2 finish

    echo '<div id="newmember3" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '03_new_role', array(
        'type'          => 'text',
        'class'         => array( '03_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('03_new_role') );

    woocommerce_form_field( '03_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '03_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('03_new_fullname') );

    woocommerce_form_field( '03_new_code', array(
        'type'          => 'text',
        'class'         => array( '03_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('03_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction4()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>'; // div newmember3 finish
    
    echo '<div id="newmember4" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '04_new_role', array(
        'type'          => 'text',
        'class'         => array( '04_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('04_new_role') );

    woocommerce_form_field( '04_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '04_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('04_new_fullname') );

    woocommerce_form_field( '04_new_code', array(
        'type'          => 'text',
        'class'         => array( '04_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('04_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction5()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>'; // div newmember4 finish 
    
        echo '<div id="newmember5" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '05_new_role', array(
        'type'          => 'text',
        'class'         => array( '05_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('05_new_role') );

    woocommerce_form_field( '05_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '05_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('05_new_fullname') );

    woocommerce_form_field( '05_new_code', array(
        'type'          => 'text',
        'class'         => array( '05_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('05_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction6()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>';
    echo '</div>';
    echo '</div>'; // div newmember5 finish
    
    echo '<div id="newmember6" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '06_new_role', array(
        'type'          => 'text',
        'class'         => array( '06_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('06_new_role') );

    woocommerce_form_field( '06_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '06_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('06_new_fullname') );

    woocommerce_form_field( '06_new_code', array(
        'type'          => 'text',
        'class'         => array( '06_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('06_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction7()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>';
    echo '</div>';
    echo '</div>'; // div newmember6 finish
    
    echo '<div id="newmember7" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '07_new_role', array(
        'type'          => 'text',
        'class'         => array( '07_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('07_new_role') );

    woocommerce_form_field( '07_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '07_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('07_new_fullname') );

    woocommerce_form_field( '07_new_code', array(
        'type'          => 'text',
        'class'         => array( '07_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('07_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction8()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>';
    echo '</div>';
    echo '</div>'; // div newmember7 finish

    echo '<div id="newmember8" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '08_new_role', array(
        'type'          => 'text',
        'class'         => array( '08_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('08_new_role') );

    woocommerce_form_field( '08_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '08_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('08_new_fullname') );

    woocommerce_form_field( '08_new_code', array(
        'type'          => 'text',
        'class'         => array( '08_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('08_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction9()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>';
    echo '</div>';
    echo '</div>'; // div newmember8 finish

    echo '<div id="newmember9" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '09_new_role', array(
        'type'          => 'text',
        'class'         => array( '09_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('09_new_role') );

    woocommerce_form_field( '09_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '09_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('09_new_fullname') );

    woocommerce_form_field( '09_new_code', array(
        'type'          => 'text',
        'class'         => array( '09_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('09_new_code') );
    
    echo '<div class="col-lg-2">';
    echo '<div class="md-form form-sm">';
    echo '<a onclick="myFunction10()" class=" moreFields btn btn-sm btn-primary waves-effect waves-light">Click to Add new A.L.IM. Member</a>';
    echo '<br><br>';
    echo '</div>';
    echo '</div>';
    echo '</div>'; // div newmember9 finish

    echo '<div id="newmember10" style="display:none">  
    <h3>' . __( '', $domain ) . '</h3>';

    woocommerce_form_field( '10_new_role', array(
        'type'          => 'text',
        'class'         => array( '10_new_role form-row-wide' ),
        'label'         => __( 'Role', $domain ) ,
        
    ), $checkout->get_value('10_new_role') );

    woocommerce_form_field( '10_new_fullname', array(
        'type'          => 'text',
        'class'         => array( '10_new_fullname form-row-wide' ),
        'label'         => __( 'Name and Surname', $domain ) ,
        
    ), $checkout->get_value('10_new_fullname') );

    woocommerce_form_field( '10_new_code', array(
        'type'          => 'text',
        'class'         => array( '10_new_code form-row-wide' ),
        'label'         => __( 'A.L.IM. Membership Code', $domain ) ,
        
    ), $checkout->get_value('10_new_code') );
    
    
    echo '</div>'; // div newmember10 finish

    echo '</div>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
    echo '</div>';

     
endif;



// Save custom checkout fields in the order meta data
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_fields_in_order_meta_data', 20, 2 );
function custom_checkout_fields_in_order_meta_data( $order, $data ) 

    if ( isset($_POST['01_new_role']) && ! empty($_POST['01_new_role']) )
        $order->update_meta_data( '01_new_role', sanitize_text_field($_POST['01_new_role']) );

    if ( isset($_POST['01_new_fullname']) && ! empty($_POST['01_new_fullname']) )
        $order->update_meta_data( '01_new_fullname', sanitize_text_field($_POST['01_new_fullname']) );

    if ( isset($_POST['01_new_code']) && ! empty($_POST['01_new_code']) )
        $order->update_meta_data( '01_new_code', sanitize_text_field($_POST['01_new_code']) );

    if ( isset($_POST['02_new_role']) && ! empty($_POST['02_new_role']) )
        $order->update_meta_data( '02_new_role', sanitize_text_field($_POST['02_new_role']) );

    if ( isset($_POST['02_new_fullname']) && ! empty($_POST['02_new_fullname']) )
        $order->update_meta_data( '02_new_fullname', sanitize_text_field($_POST['02_new_fullname']) );

    if ( isset($_POST['02_new_code']) && ! empty($_POST['02_new_code']) )
        $order->update_meta_data( '02_new_code', sanitize_text_field($_POST['02_new_code']) );

    if ( isset($_POST['03_new_role']) && ! empty($_POST['03_new_role']) )
        $order->update_meta_data( '03_new_role', sanitize_text_field($_POST['03_new_role']) );

    if ( isset($_POST['03_new_fullname']) && ! empty($_POST['03_new_fullname']) )
        $order->update_meta_data( '03_new_fullname', sanitize_text_field($_POST['03_new_fullname']) );

    if ( isset($_POST['03_new_code']) && ! empty($_POST['03_new_code']) )
        $order->update_meta_data( '03_new_code', sanitize_text_field($_POST['03_new_code']) );

    if ( isset($_POST['04_new_role']) && ! empty($_POST['04_new_role']) )
        $order->update_meta_data( '04_new_role', sanitize_text_field($_POST['04_new_role']) );

    if ( isset($_POST['04_new_fullname']) && ! empty($_POST['04_new_fullname']) )
        $order->update_meta_data( '04_new_fullname', sanitize_text_field($_POST['04_new_fullname']) );

    if ( isset($_POST['04_new_code']) && ! empty($_POST['04_new_code']) )
        $order->update_meta_data( '04_new_code', sanitize_text_field($_POST['04_new_code']) );
    
    if ( isset($_POST['05_new_role']) && ! empty($_POST['05_new_role']) )
        $order->update_meta_data( '05_new_role', sanitize_text_field($_POST['05_new_role']) );

    if ( isset($_POST['05_new_fullname']) && ! empty($_POST['05_new_fullname']) )
        $order->update_meta_data( '05_new_fullname', sanitize_text_field($_POST['05_new_fullname']) );

    if ( isset($_POST['05_new_code']) && ! empty($_POST['05_new_code']) )
        $order->update_meta_data( '05_new_code', sanitize_text_field($_POST['05_new_code']) );

    if ( isset($_POST['06_new_role_role']) && ! empty($_POST['06_new_role_role']) )
        $order->update_meta_data( '06_new_role', sanitize_text_field($_POST['06_new_role']) );

    if ( isset($_POST['06_new_fullname']) && ! empty($_POST['06_new_fullname']) )
        $order->update_meta_data( '06_new_fullname', sanitize_text_field($_POST['06_new_fullname']) );

    if ( isset($_POST['06_new_code']) && ! empty($_POST['06_new_code']) )
        $order->update_meta_data( '06_new_code', sanitize_text_field($_POST['06_new_code']) );

        if ( isset($_POST['07_new_role']) && ! empty($_POST['07_new_role']) )
        $order->update_meta_data( '07_new_role', sanitize_text_field($_POST['07_new_role']) );

    if ( isset($_POST['07_new_fullname']) && ! empty($_POST['07_new_fullname']) )
        $order->update_meta_data( '07_new_fullname', sanitize_text_field($_POST['07_new_fullname']) );

    if ( isset($_POST['07_new_code']) && ! empty($_POST['07_new_code']) )
        $order->update_meta_data( '07_new_code', sanitize_text_field($_POST['07_new_code']) );

    if ( isset($_POST['08_new_role']) && ! empty($_POST['08_new_role']) )
        $order->update_meta_data( '08_new_role', sanitize_text_field($_POST['08_new_role']) );

    if ( isset($_POST['08_new_fullname']) && ! empty($_POST['08_new_fullname']) )
        $order->update_meta_data( '08_new_fullname', sanitize_text_field($_POST['08_new_fullname']) );

    if ( isset($_POST['08_new_code']) && ! empty($_POST['08_new_code']) )
        $order->update_meta_data( '08_new_code', sanitize_text_field($_POST['08_new_code']) );

        if ( isset($_POST['09_new_role']) && ! empty($_POST['09_new_role']) )
        $order->update_meta_data( '09_new_role', sanitize_text_field($_POST['09_new_role']) );

    if ( isset($_POST['09_new_fullname']) && ! empty($_POST['09_new_fullname']) )
        $order->update_meta_data( '09_new_fullname', sanitize_text_field($_POST['09_new_fullname']) );

    if ( isset($_POST['09_new_code']) && ! empty($_POST['09_new_code']) )
        $order->update_meta_data( '09_new_code', sanitize_text_field($_POST['09_new_code']) );

        if ( isset($_POST['10_new_role']) && ! empty($_POST['10_new_role']) )
        $order->update_meta_data( '10_new_role', sanitize_text_field($_POST['10_new_role']) );

    if ( isset($_POST['10_new_fullname']) && ! empty($_POST['10_new_fullname']) )
        $order->update_meta_data( '10_new_fullname', sanitize_text_field($_POST['10_new_fullname']) );

    if ( isset($_POST['10_new_code']) && ! empty($_POST['10_new_code']) )
        $order->update_meta_data( '10_new_code', sanitize_text_field($_POST['10_new_code']) );  

【讨论】:

以上是关于将自定义结帐字段(用户输入)添加到订单数据和用户元数据的主要内容,如果未能解决你的问题,请参考以下文章

WooCommerce:将自定义 Metabox 添加到管理订单页面

php 将自定义字段添加到购物车/结帐

woocommerce 自定义结帐字段以增加订购 ajax 的费用

在产品中添加自定义字段以显示在购物车、结帐和订单中

在 Woocommerce 购物车、结帐页面、订单和电子邮件通知中的产品标题上方显示品牌名称

如何将自定义字段添加到 WooCommerce 注册表单