Я использую плагин Woo Commerce для бронирования туров, но я хочу, чтобы имя каждого человека указывалось индивидуально на странице оформления заказа в зависимости от количества человек. Я нашел сообщение о переполнении стека, которое не сработало для меня, оно говорит, что неопределенное смещение 0 как ошибка на странице оформления заказа.

Я уже пробовал эту функцию.

//* Add a new checkout field add_filter( 'woocommerce_checkout_fields', 'ppp_filter_checkout_fields' ); function ppp_filter_checkout_fields($fields){ $fields['extra_fields'] = array( 'participant_details' ={amp}gt; array( 'type' ={amp}gt; 'participant_details', 'required' ={amp}gt; false, 'label' ={amp}gt; __( 'Participant Details' ) ), ); // Add a "persons" hidden input field foreach( WC()-{amp}gt;cart-{amp}gt;get_cart() as $cart_item ) { $persons = $cart_item['booking']['_persons'][0]; } echo '{amp}lt;input type="hidden" name="persons" value="' . $persons . '"{amp}gt;'; return $fields; } //* Add the field to the checkout add_filter( 'woocommerce_form_field_participant_details', 'ppp_filter_checkout_field_group', 10, 4 ); function ppp_filter_checkout_field_group( $field, $key, $args, $value ){ $op_cart_count = WC()-{amp}gt;cart-{amp}gt;get_cart_contents_count(); $items = WC()-{amp}gt;cart-{amp}gt;get_cart(); foreach($items as $item) { $person = $item['booking']['_persons'][0]; } if ($person {amp}gt; 1) { $html = ''; $html .= "{amp}lt;h3{amp}gt;Deelnemers{amp}lt;/h3{amp}gt;"; for ( $i = 1; $i {amp}lt; $person; $i  ) { $counter = $i   1; $html .= "Deelnemer ". $counter . "{amp}lt;br{amp}gt;"; $html .= woocommerce_form_field( "participant_details[$i][full_name]", array( "type" ={amp}gt; "text", "return" ={amp}gt; true, "value" ={amp}gt; "", "required" ={amp}gt; true, "label" ={amp}gt; __( "Naam" ) ) ); $html .= woocommerce_form_field( "participant_details[$i][email_address]", array( "type" ={amp}gt; "email", "return" ={amp}gt; true, "value" ={amp}gt; "", "required" ={amp}gt; true, "label" ={amp}gt; __( "Emailadres" ) ) ); } return $html; } } //* display the extra field on the checkout form add_action( 'woocommerce_checkout_after_customer_details' ,'ppp_extra_checkout_fields' ); function ppp_extra_checkout_fields(){ $checkout = WC()-{amp}gt;checkout(); foreach ( $checkout-{amp}gt;checkout_fields['extra_fields'] as $key ={amp}gt; $field ) : woocommerce_form_field( $key, $field, $checkout-{amp}gt;get_value( $key ) ); endforeach; } //* Validate custom checkout fields add_action( 'woocommerce_after_checkout_validation', 'ppp_checkout_field_participant_details_validation', 20, 2 ); function ppp_checkout_field_participant_details_validation( $data, $errors ){ if ( isset( $_POST['persons'] ) {amp}amp;{amp}amp; $_POST['persons'] {amp}gt; 1 ){ for ( $i = 1, $j = 2; $i {amp}lt; esc_attr( $_POST['persons'] ); $i  , $j   ) { if ( empty( $_POST['participant_details'][$i]['full_name'] ) ) $errors-{amp}gt;add( 'participant_'.$i.'_full_name', __( "Please fill in the participant $j full name" ), 'error' ); if ( empty( $_POST['participant_details'][$i]['email_address'] ) ) $errors-{amp}gt;add( 'participant_'.$i.'_email_address', __( "Please fill in the participant $j email address" ), 'error' ); } } } //* Sanitize our custom field function ppp_custom_process_checkout_field_participant_details( $posted ){ $clean = array(); foreach( $posted as $participant ){ $details = ppp_custom_checkout_clean_participant_details( $participant ); if( ! empty( $details ) ){ $clean[] = $details; } } return $clean; } add_filter( 'woocommerce_process_checkout_participant_details_field', 'ppp_custom_process_checkout_field_participant_details' ); //* function ppp_custom_checkout_clean_participant_details( $participant = array() ){ $details = array(); if( isset( $participant["full_name"] ) ){ $details['full_name'] = sanitize_text_field( $participant["full_name"] ); } if( isset( $participant["email_address"] ) ){ $details['email_address'] = sanitize_text_field( $participant["email_address"] ); } return $details; } //* Update_post_meta add_action( 'woocommerce_checkout_update_order_meta', 'ppp_custom_checkout_field_update_order_meta', 10, 2 ); function ppp_custom_checkout_field_update_order_meta( $order_id, $posted ){ if( ! empty( $posted["participant_details"] ) ){ update_post_meta( $order_id, "_participant_details", $posted["participant_details"] ); } else { delete_post_meta( $order_id, "_participant_details" ); } } add_action( 'woocommerce_checkout_update_order_meta', 'ppp_custom_checkout_field_update_order_meta', 10, 2 ); 

Вот сайт, где я пытаюсь достичь этого. Интернет сайт

  // 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()-{amp}gt;cart-{amp}gt;get_cart() as $cart_item){ $index  ; // 2nd Loop through each unit related to item quantity for($i = 1; $i {amp}lt;= $cart_item['booking']['Adults']; $i  ){ woocommerce_form_field("Adult[$index][$i]", array( 'type' ={amp}gt;'text', 'class'={amp}gt;array('my-field-class form-row-wide'), 'label'={amp}gt;__('Adult Name')." ($i)", 'placeholder'={amp}gt;__('Enter adult name'), ), $checkout-{amp}gt;get_value("Adult[$index][$i]")); } for($i = 1; $i {amp}lt;= $cart_item['booking']['Childs']; $i  ){ woocommerce_form_field("Child[$index][$i]", array( 'type' ={amp}gt;'text', 'class'={amp}gt;array('my-field-class form-row-wide'), 'label'={amp}gt;__('Child Name')." ($i)", 'placeholder'={amp}gt;__('Enter child name'), ), $checkout-{amp}gt;get_value("Child[$index][$i]")); } } }