WooCommerce function.php - how to save coupon code to a custom field? - php

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

Related

Save multiple WooCommerce custom checkout fields as order meta data

Im looking to find some php code that will allow me to extract information (Name, address etc) from the checkout fields and add it to the order meta data.
Looking for this to be a simple as possible
Ive previously found this code which allows a custom box to be added to the checkout page, and I sort of understand how it works, however I want to capture their name when they type it into the billing first name box. I can seem to grasp how to capture this data and put it into the order meta data, Ive tried shortening the code and editing it a few times but I dont seem to be winning
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
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){
echo '<p><strong>'.__('Phone From Checkout Form').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
/**
* 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>';
}
/**
* 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( __( 'Please enter something into this new shiny field.' ), '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'] ) );
}
}
I like this and it does work but just not in the way i need it to work. thanks for any and all help
There are some mistakes and missing things in your code… Try the following replacement code instead:
// Add shipping phone (in checkout and My account edit shipping address) and save field value
add_action( 'woocommerce_shipping_fields', 'add_shipping_phone_field' );
function add_shipping_phone_field( $fields ) {
$fields['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
// Display shipping phone value on the order edit pages under shipping section
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_shipping_phone_in_admin_orders' );
function display_shipping_phone_in_admin_orders( $order ){
$phone_value = $order->get_meta('_shipping_phone');
if ( ! empty($phone_value) ) {
echo '<p><strong>'.__('Shipping phone').':</strong> ' . $phone_value . '</p>';
}
}
// Add a custom checkout field
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_slug', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('My custom field'),
'placeholder' => __('Enter something… '),
'required' => true,
), $checkout->get_value( 'my_field_slug' ) );
echo '</div>';
}
// Validate required checkout fields
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 ( isset($_POST['my_field_slug']) && empty($_POST['my_field_slug']) ) {
wc_add_notice( __( '"My custom field" is a required field.' ), 'error' );
}
}
// Add custom checkout field value as custom order meta data
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order ) {
if ( isset($_POST['my_field_slug']) && ! empty($_POST['my_field_slug']) ) {
$order->update_meta_data( 'My Field', sanitize_text_field( $_POST['my_field_slug'] ) );
}
}
// Display "My field" value on the order edit pages under billing section
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_my_custom_checkout_field_in_admin_orders', 10, 1 );
function display_my_custom_checkout_field_in_admin_orders($order){
$my_field_value = $order->get_meta('My Field');
if ( ! empty($my_field_value) ) {
echo '<p><strong>'.__('My field').':</strong> ' . $my_field_value . '</p>';
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Now If you need to extract some data from Woocommerce existing checkout fields and combine it in a custom way to save it as custom order meta data, try to be more explicit:
What fields?
How you want to combine it?
What is the custom field slug to be used to save that data combination?

How to print a custom checkout field into email order details

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
}

Display a custom-field value in Woocommerce orders edit view

I have a similar like in here: I don't understand why the value of this custom field is not saved. Here's my code:
add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );
function altri_campi( $fields ) {
$fields['billing']['codice_fiscale'] = array(
'class' => array('form-row-wide'),
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide')
);
return $fields;
}
// like LoizTheAztec above
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['codice_fiscale'] ) ) {
update_post_meta( $order_id, 'Codice Fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
}
}
// then I'm expecting that custom field value will be saved somehow, but won't
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){
echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_codice_fiscale', true ) . '</p>';
}
Meta field is correctly saved and printed, but I cannot add the custom field value in the order data view.
What I am doing wrong and how to display this custom field value in the Order edit view?
Otherwise the text I expect to find after Customer's billing and shipping data is void, since I read only the html part of the latest snippet.
Here is the correct commented and explained code:
// Creating and displaying the custom checkout field in checkout page
add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );
function altri_campi( $fields ) {
$fields['billing']['codice_fiscale'] = array(
'class' => array('form-row-wide'),
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide')
);
return $fields;
}
// Saving the custom checkout field value in the order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['codice_fiscale'] ) ){
update_post_meta( $order_id, 'codice_fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
// get the customer ID
$customer_id = get_post_meta( $order_id, '_customer_user', true );
// Update customer user data
update_user_meta( $customer_id, 'codice_fiscale', true );
}
}
// Displaying the custom checkout field value in the order edit page (backend)
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
$codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true );
if( ! empty( $codice_fiscale ))
echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . $codice_fiscale . '</p>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. You will get in backend order edit page something like (below addresses):

Required custom WooCommerce checkout fields don't validate entered value

I'm adding WooCommerce custom checkout fields in a Storefront child theme functions.php file.
They have a "required" attribute.
The aim is to have these fields appear at the top of the page, before the billing fields.
when clicking the submit button to proceed to payment, I'm getting the required custom field validation error ('please fill in your name') and can't continue with payment, even though filling the field with valid data.
Any clue how to fix this or where to start debugging ?
here is the code in functions.php:
add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );
function my_custom_checkout_fields( $checkout ) {
echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';
woocommerce_form_field( 'developer_name', array(
'type' => 'text',
'class' => array('developer_name-class form-row form-row-first'),
'label' => __('name'),
'placeholder' => __('fill in your name'),
'required' => true,
), $checkout->get_value( 'developer_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['developer_name'] )
wc_add_notice( __( 'please fill in your name' ), 'error' );
}
I tried the following but none of them helped:
1. changing:
if ( ! $_POST['developer_name'] )
to
if ( empty( $_POST['developer_name']) )
2. changing the trigger from:
add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );
to
add_action( 'woocommerce_after_checkout_form', 'my_custom_checkout_fields' );
3. updating to latest Woocomerce 3.0.5 version
I'm running Wordpress 4.7.4
additional related active plugins:
Uni CPO - WooCommerce Options and Price Calculation Formulas
As you can read in woocommerce_before_checkout_form hook, it's before checkout form (so outside the checkout form). For this reason this custom field can't work in this hook.
You can use instead woocommerce_checkout_update_order_meta action hook, making some little changes in your code as there is no $checkout argument available in it.
This will display the field "at the top of the page, before the billing fields"…
So your complete code should be now:
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_checkout_before_customer_details', 'my_custom_checkout_fields' );
function my_custom_checkout_fields() {
echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';
woocommerce_form_field( 'developer_name', array(
'type' => 'text',
'class' => array('developer_name-class form-row form-row-first'),
'label' => __('name'),
'placeholder' => __('fill in your name'),
'required' => true,
), WC()->checkout->get_value( 'developer_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['developer_name'] )
wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['developer_name'] ) ) {
update_post_meta( $order_id, 'Developer name', sanitize_text_field( $_POST['developer_name'] ) );
}
}
// Display the custom-field in orders view
add_action( 'woocommerce_order_details_after_customer_details', 'display_my_custom_field_in_orde_details', 10, 1 );
function display_my_custom_field_in_orde_details( $order ) {
$developer_name = get_post_meta( $order->get_id(), 'Developer name', true );
if ( ! empty( $developer_name ) ):
?>
<table class="woocommerce-table woocommerce-table--customer-details shop_table customer_details">
<tbody><tr>
<th>Developer name:</th>
<td><?php echo $developer_name; ?></td>
</tr></tbody>
</table>
<?php
endif;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 3.0+
You try this code. Tested Ok
add_action( 'woocommerce_billing_fields', 'my_custom_checkout_fields' );
function my_custom_checkout_fields( $fields ) {
$fields['billing_developer_name'] = array(
'label' => __('Developer name', 'woocommerce'),
'placeholder' => _x('Developer name', 'placeholder', 'woocommerce'),
'required' => TRUE,
'clear' => false,
'type' => 'text',
'class' => array('my-css')
);
return $fields;
}
You can arrange it with this snippet
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
$order = array(
"billing_developer_name",
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_country",
"billing_email",
"billing_phone"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
See screenshot
**/* If You Have Created Your Custom Field at the checkout page */**
add_action( 'woocommerce_after_checkout_validation', 'shipping_time_optionss', 9999, 2);
function shipping_time_optionss( $fields, $errors ){
// if any validation errors
if ( empty( $_POST['woo_shipping_time'] ) ) {
$errors->add( 'woocommerce_password_error', __( 'Please Select Shipping Time Option.' ) );
} `enter code here`
}

Woocommerce custom field not saving

I have 3 custom fields in woocommerce:
Matter reference number, invoice number and soliciter dealing with case (code next, if you dont want to see it just scroll down, its not that important)
add_action('woocommerce_after_order_notes', 'matter_reference_number_func');
function matter_reference_number_func( $checkout ) {
echo '<div id="matter_ref"><h3>'.__('Matter reference number').'</h3>';
woocommerce_form_field( 'matter_reference_number', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Matter reference number'),
), $checkout->get_value( 'matter_ref_num' ));
echo '</div>';
}
add_action('woocommerce_after_order_notes', 'invoice_number_func');
function invoice_number_func( $checkout ) {
echo '<div id="inv_num"><h3>'.__('Invoice number').'</h3>';
woocommerce_form_field( 'invoice_number', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Invoice number'),
), $checkout->get_value( 'invoice_num' ));
echo '</div>';
}
add_action('woocommerce_after_order_notes', 'sol_deal_func');
function sol_deal_func( $checkout ) {
echo '<div id="sol_deal"><h3>'.__('Solicitor dealing with matter').'</h3>';
woocommerce_form_field( 'matter_reference_number', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Solicitor dealing with matter'),
), $checkout->get_value( 'sol_deal' ));
echo '</div>';
}
Now however when I try to save the values they do not appear in the email to client nor in the admin, or even on confirm page. According the the docs this should be sufficient to do that; but it isnt.
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'matter_ref_num_checkout_field_update_order_meta');
function matter_ref_num_checkout_field_update_order_meta( $order_id ) {
if ($_POST['matter_ref_num']) update_post_meta( $order_id, 'Matter Reference Number', esc_attr($_POST['matter_ref_num']));
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'invoice_num_checkout_field_update_order_meta');
function invoice_num_checkout_field_update_order_meta( $order_id ) {
if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'sol_deal_checkout_field_update_order_meta');
function sol_deal_checkout_field_update_order_meta( $order_id ) {
if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}
You are using the hook to update the post meta (being the post id the order id). If you check your database you should be able to see those fields under the appropriate post id meta.
If you want to add them to the emails you'll need to use another hook too:
woocommerce_email_order_meta_keys
Here is a reference from woocommerce documentation:
/**
* Add the field 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 ) {
$keys[] = 'My Field';
return $keys;
}
You really only need to use one hook per action and place your code there, eg:
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
function custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['matter_ref_num']) update_post_meta( $order_id, 'Matter Reference Number', esc_attr($_POST['matter_ref_num']));
if ($_POST['invoice_num']) update_post_meta( $order_id, 'Invoice Number', esc_attr($_POST['invoice_num']));
if ($_POST['sol_deal']) update_post_meta( $order_id, 'Solicitor Dealing With Matter', esc_attr($_POST['sol_deal']));
}
Hope this helps.
The accepted answer didn't work for me. I had to remove the second parameter of the update_post_meta function:
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_fields_update_order_meta' );
function custom_checkout_fields_update_order_meta( $order_id ) {
if ($_POST['_billing_vat_number']) update_post_meta( $order_id, '_billing_vat_number', sanitize_text_field( $_POST['_billing_vat_number'] ) );
}
I also used a sanitize function form wordpress, but that wasn't the issue.

Categories