Good to know: I have created additional fields based on the quantity of the product. So if someone purchases the product twice, he has to fill in two fields during the checkout. If someone buys the product 1x, they must fill in the field once.
These extra fields contain "select options". I need to get the meta data of this in my order overview and in the emails from woocommerce.
//Custom WooCommerce Checkout Fields based on Quantity
add_action( 'woocommerce_before_order_notes', 'person_details' );
function person_details($checkout) {
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
for($k=1; $k<= $count; $k ) {
$i ;
print ('Gegevens van kind '.$i.'');
woocommerce_form_field( 'cstm_full_name'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Volledige naam'),
'placeholder' => __(''),
'required' => true,
),
$checkout->get_value( 'cstm_full_name'.$i ));
echo '<div ></div>';
woocommerce_form_field( 'cstm_gender', array(
'type' => 'select',
'required' => true,
'class' => array( 'my-field-class form-row-wide' ),
'label' => __( 'Jongen, Meisje of Genderneutraal' ),
'options' => array(
'blank' => __( 'Selecteer', 'njengah' ),
'jongen' => __( 'Jongen', 'njengah' ),
'meisje' => __( 'Meisje', 'njengah' ),
'genderneutraal' => __( 'Genderneutraal', 'njengah' )
)),
$checkout->get_value( 'cstm_gender' ));
woocommerce_form_field( 'cstm_leeftijd'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __('Leeftijd bij aanvang kamp'),
'placeholder' => __(''),
'required' => true,
),
$checkout->get_value( 'cstm_leeftijd'.$i ));
woocommerce_form_field( 'cstm_groep'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-last'),
'label' => __('In welke huidige Groep'),
'placeholder' => __(''),
'required' => true,
),
$checkout->get_value( 'cstm_groep'.$i ));
woocommerce_form_field( 'cstm_shirt', array(
'type' => 'select',
'required' => true,
'class' => array( 'my-field-class form-row-first' ),
'label' => __( 'Shirt size' ),
'options' => array(
'blank' => __( 'Selecteer', 'njengah' ),
'Maat S' => __( 'S', 'njengah' ),
'Maat M' => __( 'M', 'njengah' ),
'Maat L' => __( 'L', 'njengah' )
)),
$checkout->get_value( 'cstm_shirt' ));
woocommerce_form_field( 'cstm_ervaring', array(
'type' => 'select',
'required' => true,
'class' => array( 'my-field-class form-row-last' ),
'label' => __( 'Summer Camp experience' ),
'options' => array(
'blank' => __( 'Selecteer', 'njengah' ),
'1e keer Summer Camp' => __( '1e keer Summer Camp', 'njengah' ),
'2e keer Summer Camp' => __( '2e keer Summer Camp', 'njengah' ),
'3e keer Summer Camp' => __( '3e keer Summer Camp', 'njengah' ),
'4e keer Summer Camp' => __( '4e keer Summer Camp', 'njengah' )
)),
$checkout->get_value( 'cstm_ervaring' ));
echo '<div ></div>';
}
}
/**
* Save value of fields
*/
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
function customise_checkout_field_update_order_meta($order_id)
{
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
for($k=1; $k<= $count; $k ) {
$i ;
if (!empty($_POST['cstm_full_name'.$i])) {
update_post_meta($order_id, 'Naam van kind'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
}
if (!empty($_POST['cstm_gender'.$i])) {
update_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, sanitize_text_field($_POST['cstm_gender'.$i]));
}
if (!empty($_POST['cstm_leeftijd'.$i])) {
update_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, sanitize_text_field($_POST['cstm_leeftijd'.$i]));
}
if (!empty($_POST['cstm_groep'.$i])) {
update_post_meta($order_id, 'In welke huidige Groep'.$i, sanitize_text_field($_POST['cstm_groep'.$i]));
}
if ( isset( $_POST['cstm_groep'.$i] )){
$value = sanitize_text_field( $_POST['cstm_groep'.$i] );
update_post_meta( $post->ID, 'cstm_groep'.$i, $value );
}
}
}
/**
* Add fields to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$i = 0;
for($k=1; $k<= 50; $k ) {
$i ;
$keys[] = 'Naam van kind'.$i;
$keys[] = 'Jongen, Meisje of Genderneutraal'.$i;
$keys[] = 'Leeftijd bij aanvang kamp'.$i;
$keys[] = 'In welke huidige Groep'.$i;
$keys[] = '_cstm_gender'.$i;
$keys[] = '_cstm_shirt'.$i;
$keys[] = '_cstm_ervaring'.$i;
}
return $keys;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can use the woocommerce_email_order_meta
action hook to add your custom meta to the email and the woocommerce_admin_order_data_after_order_details
hook for displaying custom meta on the overview page. try the below code.
function person_details( $checkout ) {
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
for( $k=1; $k<= $count; $k ) {
$i ;
print ('Gegevens van kind '.$i.'');
woocommerce_form_field( 'cstm_full_name'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Volledige naam'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'cstm_full_name'.$i ));
echo '<div ></div>';
woocommerce_form_field( 'cstm_gender'.$i, array(
'type' => 'select',
'required' => true,
'class' => array( 'my-field-class form-row-wide' ),
'label' => __( 'Jongen, Meisje of Genderneutraal' ),
'options' => array(
'blank' => __( 'Selecteer', 'njengah' ),
'jongen' => __( 'Jongen', 'njengah' ),
'meisje' => __( 'Meisje', 'njengah' ),
'genderneutraal' => __( 'Genderneutraal', 'njengah' )
)), $checkout->get_value( 'cstm_gender'.$i ));
woocommerce_form_field( 'cstm_leeftijd'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-first'),
'label' => __('Leeftijd bij aanvang kamp'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'cstm_leeftijd'.$i ));
woocommerce_form_field( 'cstm_groep'.$i, array(
'type' => 'text',
'class' => array('my-field-class form-row-last'),
'label' => __('In welke huidige Groep'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'cstm_groep'.$i ));
woocommerce_form_field( 'cstm_shirt', array(
'type' => 'select',
'required' => true,
'class' => array( 'my-field-class form-row-first' ),
'label' => __( 'Shirt size' ),
'options' => array(
'blank' => __( 'Selecteer', 'njengah' ),
'Maat S' => __( 'S', 'njengah' ),
'Maat M' => __( 'M', 'njengah' ),
'Maat L' => __( 'L', 'njengah' )
)), $checkout->get_value( 'cstm_shirt' ));
woocommerce_form_field( 'cstm_ervaring', array(
'type' => 'select',
'required' => true,
'class' => array( 'my-field-class form-row-last' ),
'label' => __( 'Summer Camp experience' ),
'options' => array(
'blank' => __( 'Selecteer', 'njengah' ),
'1e keer Summer Camp' => __( '1e keer Summer Camp', 'njengah' ),
'2e keer Summer Camp' => __( '2e keer Summer Camp', 'njengah' ),
'3e keer Summer Camp' => __( '3e keer Summer Camp', 'njengah' ),
'4e keer Summer Camp' => __( '4e keer Summer Camp', 'njengah' )
)), $checkout->get_value( 'cstm_ervaring' ));
echo '<div ></div>';
}
}
add_action( 'woocommerce_before_order_notes', 'person_details' );
function customise_checkout_field_update_order_meta($order_id)
{
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 0;
for($k=1; $k<= $count; $k ) {
$i ;
if (!empty($_POST['cstm_full_name'.$i])) {
update_post_meta($order_id, 'Naam van kind'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
}
if (!empty($_POST['cstm_gender'.$i])) {
update_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, sanitize_text_field($_POST['cstm_gender'.$i]));
}
if (!empty($_POST['cstm_leeftijd'.$i])) {
update_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, sanitize_text_field($_POST['cstm_leeftijd'.$i]));
}
if (!empty($_POST['cstm_groep'.$i])) {
update_post_meta($order_id, 'In welke huidige Groep'.$i, sanitize_text_field($_POST['cstm_groep'.$i]));
}
if ( isset( $_POST['cstm_groep'.$i] )){
$value = sanitize_text_field( $_POST['cstm_groep'.$i] );
update_post_meta( $post->ID, 'cstm_groep'.$i, $value );
}
}
}
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
function add_email_custom_order_meta( $order, $sent_to_admin, $plain_text ){
$quantity = 0;
foreach ( $order->get_items() as $item_id => $item ) {
$quantity = $quantity $item->get_quantity();
}
$order_id = $order->get_order_number();
echo "<ul>";
$i = 0;
for( $k=1; $k <= $quantity; $k ) {
$i ;
echo "<li>Volledige naam: ".get_post_meta($order_id, 'Naam van kind'.$i, true )."</li>";
echo "<li>Jongen, Meisje of Genderneutraal: ".get_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, true )."</li>";
echo "<li>Leeftijd bij aanvang kamp: ".get_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, true )."</li>";
echo "<li>In welke huidige Groep: ".get_post_meta($order_id, 'In welke huidige Groep'.$i, true )."</li>";
echo "<li>In welke huidige Groep: ".get_post_meta( $post->ID, 'cstm_groep'.$i, true )."</li>";
}
echo "</ul>";
}
add_action( 'woocommerce_email_order_meta', 'add_email_custom_order_meta', 10, 3 );
// display the extra data in the order admin panel
function display_order_custom_data_in_admin_order_overview_page( $order ){ ?>
<div class="order_data_column" style="width: 100% !important;">
<h4><?php _e( 'Your label' ); ?></h4>
<?php
$quantity = 0;
foreach ( $order->get_items() as $item_id => $item ) {
$quantity = $quantity $item->get_quantity();
}
$order_id = $order->get_order_number();
echo "<ul>";
$i = 0;
for( $k=1; $k <= $quantity; $k ) {
$i ;
echo "<li>Volledige naam: ".get_post_meta($order_id, 'Naam van kind'.$i, true )."</li>";
echo "<li>Jongen, Meisje of Genderneutraal: ".get_post_meta($order_id, 'Jongen, Meisje of Genderneutraal'.$i, true )."</li>";
echo "<li>Leeftijd bij aanvang kamp: ".get_post_meta($order_id, 'Leeftijd bij aanvang kamp'.$i, true )."</li>";
echo "<li>In welke huidige Groep: ".get_post_meta($order_id, 'In welke huidige Groep'.$i, true )."</li>";
echo "<li>In welke huidige Groep: ".get_post_meta( $post->ID, 'cstm_groep'.$i, true )."</li>";
}
echo "</ul>";
?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_custom_data_in_admin_order_overview_page' );
Tested and works.
Order overview page