I want to gender filed required in the woocommerce. I placed the field. It creates a gender field but I was unable to create gender field required. Please, suggest me any correction.
add_action( 'woocommerce_after_checkout_billing_form','my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">
' . __('Gender') . '';
woocommerce_form_field( 'city_custom', array(
'required' => 'true',
'clear' =>'true',
'type' => 'select',
'options' => array(
'' => __( 'Select city' ),
'Kiribathgoda' => __('Male', 'woocommerce' ),
'Wattala' => __('Female', 'woocommerce' )),
'class' => array('my-field-class form-row-wide'),
), $checkout->get_value( 'city_custom' ));
echo '</div>';
}
// Save the dropdown custom field selected value as order custom meta data:
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
if ( isset($_POST['city_custom']) && ! empty($_POST['city_custom']) ) {
$order->update_meta_data( 'city', sanitize_text_field( $_POST['city_custom'] ) );
}
}
// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
echo '<p><strong>'.__('Gender').':</strong> ' . $order->get_meta('Gender') . '</p>';
}
// Display the custom field value on email notifications:
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p><strong>'.__('Gender').':</strong> ' . $order->get_meta('gender') . '</p>';
} ```
Related
I'd like to add a field on Check Payment that allows the order processor to insert a check number...please help! Check payment orders will only be entered by staff, not the public.
Add Field Here
I've searched all over and I'm stumped.
As per my understanding of your question, I have created this snippet which will help you with your requirement.
// add a field on the check payment type
add_filter( 'woocommerce_gateway_description', 'custom_check_payment_field', 10, 2 );
function custom_check_payment_field( $description, $payment_id ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$current_user_role = (array) $user->roles;
if ( 'cheque' === $payment_id && $current_user_role[0] == 'administrator') {
ob_start();
echo '<div class="cheque-field">';
woocommerce_form_field(
'customer_cheque_number',
array(
'type' => 'text',
'label' => __( 'Enter cheque number', 'woocommerce' ),
'class' => array( 'form-row-wide' ),
'required' => true,
),
''
);
echo '<div>';
$description .= ob_get_clean();
}
}
return $description;
}
// save check number in meta/database
add_action( 'woocommerce_checkout_create_order', 'save_customer_cheque_number_order_meta', 10, 2 );
function save_customer_cheque_number_order_meta( $order, $data ) {
if ( isset( $_POST['customer_cheque_number'] ) && ! empty( $_POST['customer_cheque_number'] ) ) {
$order->update_meta_data( '_customer_cheque_number', esc_attr( $_POST['customer_cheque_number'] ) );
}
}
// show check number on orders page (OPTONAL)
add_action( 'woocommerce_get_order_item_totals', 'display_customer_cheque_number_on_order_totals', 10, 3 );
function display_customer_cheque_number_on_order_totals( $total_rows, $order, $tax_display ) {
if ( $order->get_payment_method() === 'cheque' && $customer_cheque_number = $order->get_meta( '_customer_cheque_number' ) ) {
$sorted_total_rows = array();
foreach ( $total_rows as $key_row => $total_row ) {
$sorted_total_rows[ $key_row ] = $total_row;
if ( $key_row === 'payment_method' ) {
$sorted_total_rows['customer_cheque_number'] = array(
'label' => __( 'Cheque number', 'woocommerce' ),
'value' => esc_html( $customer_cheque_number ),
);
}
}
$total_rows = $sorted_total_rows;
}
return $total_rows;
}
// add meta box on orders on woocommerce backend
add_action( 'add_meta_boxes', 'add_meta_boxes_callback' );
if ( ! function_exists( 'add_meta_boxes_callback' ) ) {
function add_meta_boxes_callback() {
add_meta_box( 'shop_order_fields', __( 'Cheque number', 'woocommerce' ), 'orders_cheque_number_custom_meta', 'shop_order', 'side', 'core' );
}
}
// show meta field with entered check number
if ( ! function_exists( 'orders_cheque_number_custom_meta' ) ) {
function orders_cheque_number_custom_meta() {
global $post;
$check_number = get_post_meta( $post->ID, '_customer_cheque_number', true ) ? get_post_meta( $post->ID, '_customer_cheque_number', true ) : '';
?>
<input type="text" style="width:250px;margin-top:15px" readonly name="readonly_check_number" value="<?php echo $check_number; ?>"></p>
<?php
}
}
In this post I was able to see the code that made a custom field with a link work very well.
Display a selected variation custom field in WooCommerce as a pdf linked file
My problem is that I need to have three fields in each variation with different links each downloading a different datasheet.
I've tried this code, but I can't get it to save the custom fields when saving the variation:
// 1. Add custom field input # Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'ficha[' . $loop . ']',
'class' => 'short',
'label' => __( 'ficha', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'ficha', true )
) );
woocommerce_wp_text_input( array(
'id' => 'diagrama[' . $loop . ']',
'class' => 'short',
'label' => __( 'diagrama', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'diagrama', true )
) );
woocommerce_wp_text_input( array(
'id' => 'esquema[' . $loop . ']',
'class' => 'short',
'label' => __( 'esquema', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
) );
}
// -----------------------------------------
// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i )
{
$custom_field = $_POST['ficha'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'ficha', esc_attr( $custom_field ) );
}
{
$custom_field = $_POST['diagrama'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'diagrama', esc_attr( $custom_field ) );
}
{
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'esquema', esc_attr( $custom_field ) );
}
// -----------------------------------------
// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'add_custom_field_value_to_variation_data', 10, 3 );
function add_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
$variation_data['custom_field'] = $variation->get_meta('custom_field');
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/07/pdfimage-e1657626977128.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Ficha tecnica", "woocommerce") . '</div>';
}
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/07/pdfimage-e1657626977128.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Diagrama", "woocommerce") . '</div>';
}
if( ! empty($variation_data['custom_field']) ) {
$variation_data['custom_field_html'] = '<div class="woocommerce-custom_field"><img src="https://www.tualuce.eu/wp-content/uploads/2022/05/visibility-e1653592380924.png" width="50" height="50" alt="pdf_logo.png" title="' . __("External product link", "woocommerce") . '" />' . __(" Esquema", "woocommerce") . '</div>';
}
return $variation_data;
}
I have this code, based on Display custom field values of product variations to custom product tab in WooCommerce to display 3 custom field on WooCommerce variations.
In that fields I want to insert text with HTML tags and show it in shortcode with all HTML styling (tables etc...) but current code doesn't output styled text but raw text with printed HTML tags:
<div>some text etc...</div><a>some link</a>
Any suggestions what I did wrong here?
/* Add custom field input # Product Data > Variations > Single Variation */
add_action( 'woocommerce_variation_options', 'add_custom_field_cage_code_to_variations', 10, 3 );
function add_custom_field_cage_code_to_variations( $loop, $variation_data, $variation ) {
echo '<div class="cage_code_options_group options_group">';
woocommerce_wp_text_input( array(
'id' => 'cage_code[' . $loop . ']',
'label' => __( 'One-line description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code', true )
));
woocommerce_wp_textarea_input( array(
'id' => 'cage_code_part_number[' . $loop . ']',
'label' => __( 'Short Description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_part_number', true )
));
woocommerce_wp_textarea_input( array(
'id' => 'cage_code_niin_nsn_number[' . $loop . ']',
'label' => __( 'Long description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_niin_nsn_number', true )
));
echo '</div>';
}
/* Save custom field on product variation save */
add_action( 'woocommerce_save_product_variation', 'magazine_save_custom_field_variations', 10, 2 );
function magazine_save_custom_field_variations( $variation_id, $i ) {
$cage_code = $_POST['cage_code'][$i];
if ( isset( $cage_code ) ) update_post_meta( $variation_id, 'cage_code', esc_attr(
$cage_code ) );
$cage_code_part_number = $_POST['cage_code_part_number'][$i];
if ( isset( $cage_code ) ) update_post_meta( $variation_id, 'cage_code_part_number',
esc_attr( $cage_code_part_number ) );
$cage_code_niin_nsn_number = $_POST['cage_code_niin_nsn_number'][$i];
if ( isset( $cage_code_niin_nsn_number ) ) update_post_meta( $variation_id,
'cage_code_niin_nsn_number', esc_attr( $cage_code_niin_nsn_number ) );
}
function woo_cage_code_info_tab_content() {
global $product;
if ( $product->is_type( 'variable' ) ) {
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get meta
$cage_code = $variation->get_meta( 'cage_code' );
$cage_code_part_number = $variation->get_meta( 'cage_code_part_number' );
$cage_code_niin_nsn_number = $variation->get_meta(
'cage_code_niin_nsn_number' );
// Output
echo '<div class="woo_cage_code_info_tab_content
woo_cage_code_info_tab_content-' . $variation_id .'">';
if ( $cage_code ) {
echo '<p>' . $cage_code . '</p>';
}
echo '</div>';
}
?>
<script>
jQuery(document).ready(function($) {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
// Change
$( 'input.variation_id' ).change( function() {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
if( $( 'input.variation_id' ).val() != '' ) {
var var_id = $( 'input.variation_id' ).val();
// Display current
$( '.woo_cage_code_info_tab_content-' + var_id ).css( 'display', 'block' );
}
});
});
</script>
<?php
}
}
add_shortcode('woo_cage_code_info_tab_content', 'woo_cage_code_info_tab_content');
Your code contains some minor mistakes. You can use woocommerce_wp_text_input() or woocommerce_wp_textarea_input() without issues.
However, do not use esc_attr() when saving because it's for escaping HTML attributes (I assume that you as admin only have access to the backend fields) OR if you do use it, convert HTML entities back to characters at the output. In this answer I have used wp_kses_post() and wp_unslash()
The woocommerce_admin_process_variation_object replaces the outdated woocommerce_save_product_variation hook
So you get:
// Add field(s)
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
echo '<div class="cage_code_options_group options_group">';
woocommerce_wp_text_input(
array(
'id' => 'cage_code[' . $loop . ']',
'label' => __( 'One-line description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code', true )
)
);
woocommerce_wp_textarea_input(
array(
'id' => 'cage_code_part_number[' . $loop . ']',
'label' => __( 'Short Description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_part_number', true )
)
);
woocommerce_wp_textarea_input(
array(
'id' => 'cage_code_niin_nsn_number[' . $loop . ']',
'label' => __( 'Long description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_niin_nsn_number', true )
)
);
echo '</div>';
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3 );
// Save
function action_woocommerce_admin_process_variation_object( $variation, $i ) {
// Isset
if ( isset( $_POST['cage_code'][$i] ) ) {
// Store
$cage_code = wp_kses_post( wp_unslash( $_POST['cage_code'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code', $cage_code );
}
// Isset
if ( isset( $_POST['cage_code_part_number'][$i] ) ) {
// Store
$cage_code_part_number = wp_kses_post( wp_unslash( $_POST['cage_code_part_number'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code_part_number', $cage_code_part_number );
}
// Isset
if ( isset( $_POST['cage_code_niin_nsn_number'][$i] ) ) {
// Store
$cage_code_niin_nsn_number = wp_kses_post( wp_unslash( $_POST['cage_code_niin_nsn_number'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code_niin_nsn_number', $cage_code_niin_nsn_number );
}
}
add_action( 'woocommerce_admin_process_variation_object', 'action_woocommerce_admin_process_variation_object', 10, 2 );
Then I have no idea why you would use a shortcode versus a hook to render, like I did this in my answer. This can be any hook as long as it applies to the single product page:
// Display
function filter_woocommerce_product_tabs( $tabs ) {
// Adds the new tab
$tabs['cage_code_information_tab'] = array(
'title' => __( 'Cage Code Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_cage_code_info_tab_content'
);
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 100, 1 );
function woo_cage_code_info_tab_content() {
global $product;
if ( $product->is_type( 'variable' ) ) {
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get meta
$cage_code = $variation->get_meta( 'cage_code' );
$cage_code_part_number = $variation->get_meta( 'cage_code_part_number' );
$cage_code_niin_nsn_number = $variation->get_meta( 'cage_code_niin_nsn_number' );
// Output
echo '<div class="woo_cage_code_info_tab_content woo_cage_code_info_tab_content-' . $variation_id .'">';
if ( $cage_code ) {
echo '<p>Cage code: ' . $cage_code . '</p>';
}
if ( $cage_code_part_number ) {
echo '<p>Cage code part number: ' . $cage_code_part_number . '</p>';
}
if ( $cage_code_niin_nsn_number ) {
echo '<p>Cage code niin nsn_number: ' . $cage_code_niin_nsn_number . '</p>';
}
echo '</div>';
}
?>
<script>
jQuery(document).ready(function($) {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
// Change
$( 'input.variation_id' ).change( function() {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
if( $( 'input.variation_id' ).val() != '' ) {
var var_id = $( 'input.variation_id' ).val();
// Display current
$( '.woo_cage_code_info_tab_content-' + var_id ).css( 'display', 'block' );
}
});
});
</script>
<?php
}
}
Good morning,
I wrote/modified the following code to create 2 different new fileds on woocommerce (bithdate, and city of bith).
Everything works like a charm (in admin page, in my account page) but I'd like to show them on checkout page (so at the first time the customer buy something I can collect that info too [required]).
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'label' => __( 'Data di nascita', 'woocommerce' ),
'placeholder' => __( 'Data di nascita', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
woocommerce_form_field( 'birthcity_field', array(
'type' => 'text',
'label' => __( 'Città di nascita', 'woocommerce' ),
'placeholder' => __( 'Città di nascita', 'woocommerce' ),
'required' => true,
), get_user_meta( get_current_user_id(), 'birthcity_field', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
$args->add( 'error', __( 'Prego inserire data di nascita', 'woocommerce' ) );
}
if ( isset($_POST['birthcity_field']) && empty($_POST['birthcity_field']) ) {
$args->add( 'error', __( 'Prego inserire città di nascita', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {
if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
if( isset($_POST['birthcity_field']) && ! empty($_POST['birthcity_field']) ) {
update_user_meta( $user_id, 'birthcity_field', sanitize_text_field($_POST['birthcity_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
// Add field - admin
function add_user_birtday_field( $user ) {
?>
<h3><?php _e('Dati aggiuntivi','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="birthday_field"><?php _e( 'Data di nascita', 'woocommerce' ); ?></label></th>
<td><input type="date" name="birthday_field" value="<?php echo esc_attr( get_the_author_meta( 'birthday_field', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<table class="form-table">
<tr>
<th><label for="birthcity_field"><?php _e( 'Città di nascita', 'woocommerce' ); ?></label></th>
<td><input type="text" name="birthcity_field" value="<?php echo esc_attr( get_the_author_meta( 'birthcity_field', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'show_user_profile', 'add_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birthcity_field', 10, 1 );
// Save field - admin
function save_user_birtday_field( $user_id ) {
if( ! empty($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
}
if( ! empty($_POST['birthcity_field']) ) {
update_user_meta( $user_id, 'birthcity_field', sanitize_text_field( $_POST['birthcity_field'] ) );
}
}
add_action( 'personal_options_update', 'save_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birtday_field', 10, 1 );
add_action( 'personal_options_update', 'save_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birthcity_field', 10, 1 );
I'll be very gratefull to everyone who can help me.
Warm regards
Ale
I solved by myself inspired by this article Post on Stackoverflow, BUT as I usual do I'd like to share the solution because could be helpful for someone.
Based on the code posted above You can add the field on the checkout page with the following code:
// Add the field to the checkout
add_action( 'woocommerce_after_checkout_billing_form', 'birth_day_checkout_field' );
function birth_day_checkout_field( $checkout ) {
// if customer is logged in and his birth date is defined
if(!empty(get_user_meta( get_current_user_id(), 'birthday_field', true ))){
$checkout_birht_date = esc_attr(get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
// The customer is not logged in or his birth date is NOT defined
else {
$checkout_birht_date = $checkout->get_value( 'birthday_field' );
}
echo '<p id="custom_birthday_field">';
woocommerce_form_field( 'birthday_field', array(
'type' => 'date',
'class' => array('birth-date form-row-wide'),
'label' => __( 'Data di nascita', 'woocommerce' ),
'placeholder' => __( 'mm/dd/yyyy', 'woocommerce' ),
'required' => true,
), $checkout_birht_date);
echo '</p>';
}
// Process the checkout
add_action('woocommerce_checkout_process','birth_day_checkout_field_process');
function birth_day_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['birthday_field'] )
wc_add_notice( __( 'Inserire la <strong>Data di nascita</strong>', 'woocommerce' ), 'error' )
;
}
// update order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'birth_day_checkout_field_update_order_meta' );
function birth_day_checkout_field_update_order_meta( $order_id ) {
if (!empty( $_POST['birthday_field'])){
update_post_meta( $order_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
// updating user meta (for customer my account edit details page post data)
update_user_meta( get_post_meta( $order_id, '_customer_user', true ), 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
// update user meta with Birth date (in checkout and my account edit details pages)
add_action ( 'personal_options_update', 'birth_day_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'birth_day_save_extra_profile_fields' );
add_action( 'woocommerce_save_account_details', 'birth_day_save_extra_profile_fields' );
function birth_day_save_extra_profile_fields( $user_id )
{
// for checkout page post data
if ( isset($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
// for customer my account edit details page post data
if ( isset($_POST['birthday_field']) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
In this way this data will be updated everywhere (my account, admin and checkout).
Warm regards
Ale
How to add list of participant in check out and show in order detail page. using function and wordpress action and filters
To add field after order notes field in checkout page.
add below code in function.php to get below screen. Here we show total item in cart -1 rows. Because 1 participant is that make order.
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
global $woocommerce;
$class_basket_count = $woocommerce->cart->cart_contents_count;
$class_basket_count = $class_basket_count - 1;
if($class_basket_count >= 1){
echo '<div id="my_custom_checkout_field"><h3>' . __('Participant List') . '</h3>';
for ($i=1; $i <= $class_basket_count; $i++ ){
woocommerce_form_field( 'guest-name-'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Guest Name'),
'placeholder' => __('Enter Name'),
), $checkout->get_value( 'guest-name-'.$i ));
woocommerce_form_field( 'guest-email-'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Guest Email'),
'placeholder' => __('Enter Email'),
), $checkout->get_value( 'guest-email-'.$i ));
}
echo '</div>';
}
}
To add and update meta value of Participant List. Add below code in function.php
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
global $woocommerce;
$class_basket_count = $woocommerce->cart->cart_contents_count;
for ($i=1; $i <= $class_basket_count; $i++ ){
if ( ! empty( $_POST['guest-name-'.$i] ) ) {
update_post_meta( $order_id, 'guest-name-'.$i, sanitize_text_field( $_POST['guest-name-'.$i] ) );
update_post_meta( $order_id, 'guest-email-'.$i, sanitize_text_field( $_POST['guest-email-'.$i] ) );
}
}
}
To show list in order detail page. See screen short.
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
$totla_class_ordered = $order->get_item_count();
echo '<h4>Participant Details</h4>';
echo '<div class="participant-details"><table>';
echo '<thead><tr><th>Guest Name</th><th>Guest Email</th></tr></thead>';
for ($i=1; $i <= $totla_class_ordered; $i++ ){
echo '<tr><td>'.get_post_meta( $order->id, 'guest-name-'.$i, true ) . '</td><td>'.get_post_meta( $order->id, 'guest-email-'.$i, true ) . '</td>';
}
echo '</table></div>';
}
And for adding participant list to emails sent: (maybe helpfull to someone)
add_action('woocommerce_email_customer_details','add_list_to_emails', 15, 4 );
function add_verification_id_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
global $woocommerce;
$total_class_ordered = $order->get_item_count();
if ($total_class_ordered > 1) {
echo '<h3>Details of extra participants</h3>';
echo '<div class="participant-details" style="margin-top: 10px;"><table>';
echo '<thead><tr><th style="text-align: left;">Name</th><th style="text-align: left;">Email</th></tr></thead>';
for ( $i = 1; $i <= $total_class_ordered; $i ++ ) {
echo '<tr><td>' . get_post_meta( $order->get_id(), 'guest-name-' . $i, true ) .
'</td><td>' . get_post_meta( $order->get_id(), 'guest-email-' . $i, true ) .
'</td></tr>';
}
echo '</table></div>';
}
}