I would like print my custom checkout field into all e-mail notification, like in screenshot below.
What works so far
Add the field to the checkout
Process the checkout
Update the order meta with field value
Display field value on the order edit page
My question
How to add the custom checkout field into email order details (as seen in the screenshot)
Here is my code that I have used so far, how can I further adjust it?
/* Add the field to the checkout */
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>' . __('Imię i nazwisko osoby obdarowanej') . '</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Podaj imię i nazwisko osoby obdarowanej'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/* Process the checkout */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( '<strong>Imię i nazwisko osoby obdarowanej</strong> jest wymaganym polem' ), 'error' );
}
/* Update the order meta with field value */
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
/* Display field value on the order edit page */
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>'.__('Imie i nazwisko obdarowanej').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
}
Step 1) Change your code to this
/* Add the field to the checkout */
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>' . __('Imię i nazwisko osoby obdarowanej') . '</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Podaj imię i nazwisko osoby obdarowanej'),
'placeholder' => __(''),
'required' => true,
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/* Process the checkout */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( '<strong>Imię i nazwisko osoby obdarowanej</strong> jest wymaganym polem' ), 'error' );
}
/* Update the order meta with field value */
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_create_order', 10, 2 );
function my_custom_checkout_field_create_order( $order, $data ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
$order->update_meta_data( '_my_field', sanitize_text_field( $_POST['my_field_name'] ) ); // Order meta data
}
}
/* Display field value on the order edit page */
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) {
// Get meta
$my_field = $order->get_meta( '_my_field' );
echo '<p><strong>'.__('Imie i nazwisko obdarowanej').':</strong> ' . $my_field . '</p>';
}
Step 2) overwrite the following template file
https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/emails/email-order-details.php
This template can be overridden by copying it to
yourtheme/woocommerce/emails/email-order-details.php.
Add the following code under line number 84
$my_field = $order->get_meta( '_my_field' );
if ( isset( $my_field ) ) {
?>
<tr>
<th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo 'Imie i nazwisko obdarowanej:'; ?></th>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo $my_field; ?></td>
</tr>
<?php
}
Related
we add option to add VAT number to checkout page, it shows ok on checkout page, in admin and in emails, but it don't save to custom fields.
Here is code:
/**
* VAT Number in WooCommerce Checkout
*/
function wpdesk_vat_field( $checkout ) {
echo '<div id="wpdesk_vat_field"><h2>' . __('Nakup na podjetje') . '</h2>';
woocommerce_form_field( 'vat_number', array(
'type' => 'text',
'class' => array( 'vat-number-field form-row-wide') ,
'label' => __( 'V kolikor nakupujete kot podjetje, vnesite ID za DDV' ),
'placeholder' => __( 'Vnesite ID za DDV' ),
), $checkout->get_value( 'vat_number' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_meta', 'wpdesk_checkout_vat_number_update_order_meta' );
/**
* Save VAT Number in the order meta
*/
function wpdesk_checkout_vat_number_update_order_meta( $order_id ) {
if ( ! empty( $_POST['vat_number'] ) ) {
update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wpdesk_vat_number_display_admin_order_meta', 10, 1 );
/**
* Display VAT Number in order edit screen
*/
function wpdesk_vat_number_display_admin_order_meta( $order ) {
echo '<p><strong>' . __( 'ID za DDV', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->id, '_vat_number', true ) . '</p>';
}
add_filter( 'woocommerce_email_order_meta_keys', 'wpdesk_vat_number_display_email' );
/**
* VAT Number in emails
*/
function wpdesk_vat_number_display_email( $keys ) {
$keys['ID za DDV'] = '_vat_number';
return $keys;
}
I add this code to functions.php, but VAT number is not saved to custom fields on order page.
/**
* VAT Number in WooCommerce Checkout
*/
function wpdesk_vat_field( $checkout ) {
echo '<div id="wpdesk_vat_field"><h2>' . __('Nakup na podjetje') . '</h2>';
woocommerce_form_field( 'vat_number', array(
'type' => 'text',
'class' => array( 'vat-number-field form-row-wide') ,
'label' => __( 'V kolikor nakupujete kot podjetje, vnesite ID za DDV' ),
'placeholder' => __( 'Vnesite ID za DDV' ),
), $checkout->get_value( 'vat_number' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_meta', 'wpdesk_checkout_vat_number_update_order_meta' );
/**
* Save VAT Number in the order meta
*/
function wpdesk_checkout_vat_number_update_order_meta( $order_id ) {
if ( ! empty( $_POST['vat_number'] ) ) {
update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wpdesk_vat_number_display_admin_order_meta', 10, 1 );
/**
* Display VAT Number in order edit screen
*/
function wpdesk_vat_number_display_admin_order_meta( $order ) {
echo '<p><strong>' . __( 'ID za DDV', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->id, '_vat_number', true ) . '</p>';
}
add_filter( 'woocommerce_email_order_meta_keys', 'wpdesk_vat_number_display_email' );
/**
* VAT Number in emails
*/
function wpdesk_vat_number_display_email( $keys ) {
$keys['ID za DDV'] = '_vat_number';
return $keys;
}
I am using your code in addition I used the woocommerce_before_order_notes hook to add files and working fine for me.
/**
*
* VAT Number in WooCommerce Checkout
*/
add_action( 'woocommerce_before_order_notes', 'wpdesk_vat_field' );
function wpdesk_vat_field( $checkout ) {
echo '<div id="wpdesk_vat_field"><h2>' . __('Nakup na podjetje') . '</h2>';
woocommerce_form_field( 'vat_number', array(
'type' => 'text',
'class' => array( 'vat-number-field form-row-wide') ,
'label' => __( 'V kolikor nakupujete kot podjetje, vnesite ID za DDV' ),
'placeholder' => __( 'Vnesite ID za DDV' ),
), $checkout->get_value( 'vat_number' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_meta', 'wpdesk_checkout_vat_number_update_order_meta' );
/**
* Save VAT Number in the order meta
*/
function wpdesk_checkout_vat_number_update_order_meta( $order_id ) {
if ( ! empty( $_POST['vat_number'] ) ) {
update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wpdesk_vat_number_display_admin_order_meta', 10, 1 );
/**
* Display VAT Number in order edit screen
*/
function wpdesk_vat_number_display_admin_order_meta( $order ) {
echo '<p><strong>' . __( 'ID za DDV', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->id, '_vat_number', true ) . '</p>';
}
add_filter( 'woocommerce_email_order_meta_keys', 'wpdesk_vat_number_display_email' );
/**
* VAT Number in emails
*/
function wpdesk_vat_number_display_email( $keys ) {
$keys['ID za DDV'] = '_vat_number';
return $keys;
}
Tested and works.
Checkout
Edit Order
Mail
I have created some custom text fields to add to name field and custom message fields in cart page.So once user goes to cart page if he fills the data and click on proceed to checkout those data should be displayed in checkout page and once the customer place the order those custom fields data should be stored in orders page in admin as well.
As of now for order notes it is working fine and the field To is not working.Here is the code which i have tired.It i displaying only To as a heading in the admin panel But not displaying the name in to field in admin panel.
// Add the order_comments field to the cart
add_action( 'woocommerce_cart_collaterals', 'order_comments_custom_cart_field' );
function order_comments_custom_cart_field() {
?>
<div class="customer_notes_on_cart" style="clear:both;">
<?php
woocommerce_form_field('to_notes_text', array(
'placeholder' => __('To'),
'class' => array('form-row-last'),
'clear' => true,
), '');
?></div><?php
}
// PHP: Remove "(optional)" from non required fields
add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
// Only on cart page
if( is_cart() ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// Process the checkout and overwriting the normal button
add_action( 'woocommerce_proceed_to_checkout', 'change_proceed_to_checkout', 15 );
function change_proceed_to_checkout() {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
?>
<form id="checkout_form" method="POST" action="<?php echo wc_get_checkout_url(); ?>">
<input type="hidden" name="to_notes" id="to_notes" value="">
<button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
</form>
<?php
}
// Jquery script for cart and checkout pages
add_action('wp_footer', 'customer_notes_jquery' );
function customer_notes_jquery() {
?>
<script>
jQuery(function($) {
<?php // For cart
if( is_cart() ) : ?>
$('#to_notes_text').on( 'blur', function(){
$('#to_notes').val($(this).val());
});
<?php // For checkout
elseif( is_checkout() && ! is_wc_endpoint_url() ) : ?>
$('#to_comments' ).val("<?php echo sanitize_text_field($_POST['to_notes']); ?>");
<?php endif; ?>
});
</script>
<?php
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta',
'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
?>
<?php
if ( ! empty( $_POST['to_notes_text'] ) ) {
update_post_meta( $order_id, 'To', sanitize_text_field( $_POST['to_notes_text'] ) );
}
}
/**
* Display field value on the order edit page
*/
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){
?>
<?php
echo '<p><strong>'.__('TO').':</strong> ' . get_post_meta( $order->id, 'TO', true ) . '</p>';
}
Solved it by doing in this way
/**
* Update the order meta with field value
**/
add_action( 'woocommerce_checkout_update_order_meta',
'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
?>
<?php
if ( ! empty( $_POST['from_comments'] ) ) {
update_post_meta( $order_id, 'FROM', sanitize_text_field( $_POST['from_comments']
) );
}
}
/**
* Update the order meta with field value
**/
add_action( 'woocommerce_checkout_update_order_meta',
'my_customs_checkout_field_update_order_meta' );
function my_customs_checkout_field_update_order_meta( $order_id ) {
?>
<?php
if ( ! empty( $_POST['to_comments'] ) ) {
update_post_meta( $order_id, 'TO', sanitize_text_field( $_POST['to_comments'] )
);
}
}
/**
* Display field value on the order edit page
**/
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){
?>
<?php
echo '<p><strong>'.__('FROM').':</strong> ' . get_post_meta( $order->id, 'FROM', true
) . '</p>';
echo '<p><strong>'.__('TO').':</strong> ' . get_post_meta( $order->id, 'TO', true )
. '</p>';
}
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
?>
<?php
echo '<div id="my_custom_checkout_field"><h2>' . __('TO ') . '</h2>';
woocommerce_form_field( 'from_comments', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('FROM'),
), $checkout->get_value( 'from_comments' ));
woocommerce_form_field( 'to_comments', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('TO'),
), $checkout->get_value( 'to_comments' ));
echo '</div>';
}
I am trying to figure out how to modify the singe product options so the product admin can pick from the drop down list the condition of product, i.e. new/ used.
Below is a code that allows product admin to enter the condition of product manually.
// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array( // Text Field type
'id' => '_Stan',
'label' => __( 'Stan', 'woocommerce' ),
'placeholder' => 'i.e: nowa; uzywana...',
'desc_tip' => 'true',
'description' => __( 'Podaj stan plyty.', 'woocommerce' )
) );
echo '</div>'; // Closing </div> tag HERE
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$Stan_field = $_POST['_Stan'];
if( !empty( $Stan_field ) )
update_post_meta( $post_id, '_Stan', esc_attr( $Stan_field ) );
}
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 45);
function woo_display_custom_general_fields_values() {
global $product;
echo '<p class="custom-Stan">Stan: ' . get_post_meta( $product->id, '_Stan', true ) . '</p>';
}
There is some errors and mistakes so I have revisited a little bit your code. Now you will have to replace woocommerce_wp_text_input() by woocommerce_wp_select() to get a select field instead, this way:
// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
echo '<div class="options_group">';
woocommerce_wp_select( array( // Text Field type
'id' => '_Stan',
'label' => __( 'Stan', 'woocommerce' ),
'description' => __( 'Podaj stan plyty.', 'woocommerce' ),
'desc_tip' => true,
'options' => array(
'' => __( 'Select product condition', 'woocommerce' ),
'Nowa' => __('Nowa', 'woocommerce' ),
'Uzywana' => __('Uzywana', 'woocommerce' ),
)
) );
echo '</div>';
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields', 30, 1 );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$posted_field_value = $_POST['_Stan'];
if( ! empty( $posted_field_value ) )
update_post_meta( $post_id, '_Stan', esc_attr( $posted_field_value ) );
}
// Display In front end
add_action( 'woocommerce_product_meta_start', 'woo_display_custom_general_fields_values', 50 );
function woo_display_custom_general_fields_values() {
global $product;
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo '<span class="stan">Stan: ' . get_post_meta( $product_id, '_Stan', true ) . '</span>';
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
Is better to avoid capitals in meta keys and they should start with an underscore.
I found this code on here earlier today and I tried modifying it but I get an error code when I try to add more emails to the code (it starts with 2) not sure what I'm doing wrong.
Here is the example
This is my modified Code (tried adding a third email to the bottom and I'm getting a php error)
// Add the custom checkout field
add_filter( 'woocommerce_after_order_notes', 'restaurant_location_checkout_field' );
function restaurant_location_checkout_field( $checkout ) {
woocommerce_form_field( 'restaurant_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Select Location', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Please select an option', 'woocommerce' ),
'4289 boul St-Jean' => __('4289 boul St-Jean', 'woocommerce' ),
'3559 boul St-Charles' => __('3559 boul St-Charles', 'woocommerce' ),
'Baton Rouge' => __('Baton Rouge', 'woocommerce' )
)
), $checkout->get_value( 'restaurant_location' ));
}
// Process the checkout (checking)
add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
function restaurant_location_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['restaurant_location'] )
wc_add_notice( __( 'Please select a food option .' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta' );
function restaurant_location_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['restaurant_location'] ) ) {
update_post_meta( $order_id, '_restaurant_location', sanitize_text_field( $_POST['restaurant_location'] ) );
}
}
// Display field value on the order edit page
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>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_restaurant_location', true ) . '</p>';
}
// Conditional Email recipient filter based on restaurant location
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2 );
function conditional_email_recipient( $recipient, $order ) {
$location = get_post_meta( $order->get_id(), '_restaurant_location', true );
$recipient = $location == '4289 boul St-Jean' ? ',nicks#mtygroup.com' : ',nicsoti#yahoo.com' : ',nicks#mtygroup.com' ;
return $recipient;
}
This is the original Code I found
// Add the custom checkout field
add_filter( 'woocommerce_after_order_notes', 'restaurant_location_checkout_field' );
function restaurant_location_checkout_field( $checkout ) {
woocommerce_form_field( 'restaurant_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Food options', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Please select an option', 'woocommerce' ),
'New Orleans' => __('New Orleans', 'woocommerce' ),
'Baton Rouge' => __('Baton Rouge', 'woocommerce' )
)
), $checkout->get_value( 'restaurant_location' ));
}
// Process the checkout (checking)
add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
function restaurant_location_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['restaurant_location'] )
wc_add_notice( __( 'Please select a food option .' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta' );
function restaurant_location_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['restaurant_location'] ) ) {
update_post_meta( $order_id, '_restaurant_location', sanitize_text_field( $_POST['restaurant_location'] ) );
}
}
// Display field value on the order edit page
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>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_restaurant_location', true ) . '</p>';
}
// Conditional Email recipient filter based on restaurant location
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2 );
function conditional_email_recipient( $recipient, $order ) {
$location = get_post_meta( $order->get_id(), '_restaurant_location', true );
$recipient = $location == 'New Orleans' ? ',test1#example.com' : ',test1#example.com';
return $recipient;
}
Change this line :
$recipient = $location == '4289 boul St-Jean' ? ',nicks#mtygroup.com' : ',nicsoti#yahoo.com' : ',nicks#mtygroup.com' ;
to :
switch ($location) {
case '4289 boul St-Jean':
$recipient = ',nicks#mtygroup.com';
break;
case '3559 boul St-Charles':
$recipient = ',nicsoti#yahoo.com';
break;
default:
$recipient = ',nicks#mtygroup.com';
break;
}
Or just modify it to suits your needs
How to add Custom Product Fields in WooCommerce
I followed the guide above and add the code below to my functions.php. That worked fine. My problem is how to I modify this code to save the coupon code $_POST['coupon_code'] entered (during checkout) and save that to the custom field? I am stuck at that point, since my overall goal is to test the coupon code and fill the custom field with the salesperson that promoted that coupon code to the customers.
Thank you for any help!
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
/**
* Display field value on the order edit page
*/
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>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
}
If you are still looking for answers, I managed to spice up your code with some help from my own question as I was in a similair situation.
Fill out custom field with used coupon code WooCommerce
your code and my edits were changed to where it actually saves the coupons to a custom value. On of the problems with your code was how you tried to reach the custom field with 'My field'. I have replaced these with the actual name of the field.
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta',
'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
if ( isset($_POST['my_field_name']) && empty( $_POST['my_field_name'])) {
$order = new WC_Order( $order_id );
foreach( $order->get_used_coupons() as $coupon) {
$coupons .= $coupon.', ';
}
update_post_meta( $order_id, 'my_field_name', $coupons);
}
}
/**
* Display field value on the order edit page
*/
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>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'my_field_name', true ) . '</p>';
}
You might want to clean up a bit before using this code. ;)
Source: Fill out custom field with used coupon code WooCommerce