I have a WooCommerce custom field created dependent on product ID and the field name is incremented by 1 by quantity purchased.
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
/**
* Save value of fields
*/
function customise_checkout_field_update_order_meta($order_id) {
// True
if ( WC()->cart ) {
// Specific product IDs
$product_ids = array( 11,12,13 );
// Initialize
$count = 0;
// Loop trough cart items quantities
foreach ( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
// Checks if a value exists in an array
if ( in_array( $product_id, $product_ids ) ) {
$count += $cart_item_quantity;
}
}
$i = 0;
for($k=1; $k<= $count; $k++) {
$i++;
if (!empty($_POST['cstm_full_name'.$i])) {
update_post_meta($order_id, 'Name of Attendee '.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
}
}
}
}
That works fine, but I want to display the fields on the WooCommerce internal Order Admin screen, how can I loop through the custom field and display the incremented field names...
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bt_attendees_field_display_admin_order_meta', 10, 1 );
/**
* Display attendees value on the backend WooCommerce order
*/
function bt_attendees_field_display_admin_order_meta($order){
$i = 1;
echo '<p>Attendee Name ' . get_post_meta( $order->get_id(), 'Name of Attendee '.$i, true ) . '<p>';
}
So above the first field will be output above as the variable has been set to 1 - how can I loop though an indeterminate amount of the same field per order per order.

