woocommerce_checkout_update_order_meta action is not working
Asked Answered
F

2

9

Hi today i was working with woo-commerce and i have successfully created some custom checkout fields as per user requirements but i am unable to save them in database.

Here how i created custom checkout fields...its in child theme functions.php

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Over Ridding, Removing, Creating New Fields.
function custom_override_checkout_fields( $fields ) {
     unset($fields['billing']['billing_company']);
     unset($fields['billing']['billing_address_2']);    
     unset($fields['order']['order_comments']);
     unset($fields['billing']['billing_address_1']);
     unset($fields['billing']['billing_city']);
     unset($fields['billing']['billing_postcode']);
     unset($fields['billing']['billing_email']);


     $fields['billing']['your_name'] = array(
    'type'      => 'text',
    'label'     => __('Full Name', 'woocommerce'),
    'placeholder'   => _x('Full Name', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['your_phone_number'] = array(
    'type'      => 'text',
    'label'     => __('Your Phone Number', 'woocommerce'),
    'placeholder'   => _x('Your Phone Number', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_name'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Name", 'woocommerce'),
    'placeholder'   => _x("Recipient's Name", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_company_name'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Company (if any)", 'woocommerce'),
    'placeholder'   => _x("Recipient's Company (if any)", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_phone_number'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Phone Number", 'woocommerce'),
    'placeholder'   => _x("Recipient's Phone Number", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_address'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Address", 'woocommerce'),
    'placeholder'   => _x("Recipient's Address", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

In db where i am looking for the fields. its wp_postmeta table. Attached is screen shot i am searching with order id.. wp_postmeta db table

Now i have add the checkout_update_order_meta action to update the order meta and store my custom created fields. But it seems like that it isn't working because when i check in wp_postmeta table with latest created order id i don't find my custom fields there.

add_action( 'woocommerce_checkout_update_order_meta', 'some_custom_checkout_field_update_order_meta' );

function some_custom_checkout_field_update_order_meta( $order_id ) {


if ( ! empty( $_POST['recipient_address'] ) ) {
add_post_meta( $order_id, 'recipient_address', sanitize_text_field( $_POST['recipient_address'] ) );
}
if (!empty($_POST['recipient_phone_number'])) {
        update_post_meta($order_id, 'recipient phone number', sanitize_text_field($_POST['recipient_phone_number']));
    }

}

Its my first dealing with woocommerce code i searched a lot and came here as i give up on it. Please help me solve this mystery.

Please Correct me what i am doing wrong. Also after this step i will have to display these custom fields in wordpress dashboard under woocommerce > orders > order details so if there is any helpful link for that please provide.

Thanks in advance.

Fae answered 18/5, 2017 at 20:38 Comment(0)
V
11

I have just change a little bit your last hooked function and it works (on WC version 2.6.x and 3.0+). It's better with empty() php function to use variables (to be retro compatible).
Also is better to use update_post_meta() instead of add_post_meta() as this function will make sure that the meta_key already exists and if not, add_post_meta() will be called instead...

Here a screenshot of the wp_postmeta table related to the order meta data: wp_postmeta table

If the meta_key don't start by an underscore like here, it appears in backend order edit page in the Custom fields metabox:enter image description here

Here is this code:

add_action( 'woocommerce_checkout_update_order_meta', 'saving_checkout_cf_data');
function saving_checkout_cf_data( $order_id ) {

    $recipient_address = $_POST['recipient_address'];
    if ( ! empty( $recipient_address ) )
        update_post_meta( $order_id, 'recipient_address', sanitize_text_field( $recipient_address ) );

    $recipient_phone_number = $_POST['recipient_phone_number'];
    if ( ! empty( $recipient_phone_number ) )
        update_post_meta($order_id, 'recipient_phone_number', sanitize_text_field( $recipient_phone_number ) );

}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

If you want to have a meta_key starting by _billing… like classic billing checkout fields, you just need to change that in update_post_meta() function. For example:

update_post_meta( $order_id, '_billing_recipient_address', sanitize_text_field( $recipient_address ) );

But in this case, this will not appear in the custom Fields metabox in the order edit page.

Vento answered 18/5, 2017 at 23:29 Comment(7)
It worked like a champ so the only mistaken thing was nested function. if(! empty($_POST['PROBLEM I AM NOT GOING TO WORK AS NESTED FUNCTION'])); THANKS A LOT MATE CODE WORK LIKE A CHAMP ALSO YOU OPENED MY MIND TO SEVERAL OTHER THINGSFae
You can also always use update_post_meta() as add_post_meta() is included in it…Vento
I will take care of the things you mentioned but there is one other problem that how i am going to display the custom field information in wordpress dashboard in order menu ? Can you helpFae
For reference, this is the official dev doc for checkout custom fields: Customizing checkout fields using actions and filtersVento
I have gone through the link you sent several times. But i think the problem was that my db were not updating with custom meta key and values that's why i was not seeing the custom fields data in backend let me try now and will get back to you... BY THE WAY many thanks you are champ champ champFae
I mean wordpress dashboard > woocommerce > orders > newest order > view > and the full detail of the order for owner of shopFae
@Vento ? More like Loic The LEGEND!Corenda
X
0

i was needed upload more than one field, and i used an array like this:

add_action('woocommerce_checkout_update_order_meta', 'tu_funcion'); 
function tu_funcion($order_id) 
{ 
$arrEnv = array('billing_cif', 'despliegue_nombre', 'despliegue_apellido', 'despliegue_correo');
foreach ($arrEnv as $valor) :
if (!empty($_POST[$valor]))
update_post_meta($order_id, $valor, sanitize_text_field($_POST[$valor]));
endforeach; }

previously I introduced them to checkout with:

add_filter ('woocommerce_checkout_fields', 'custom_fields_finish_purchase');
function custom_fields_finish_purchase($fields){
 $fields['billing']['billing_cif'] = array (
     'type' => 'text',
     'label' => 'CIF',
     'placeholder' => 'Write here the CIF of the company',
     'class' => array ('form-row-wide'),
     'required' => true,
 );
// more fields here...
return $fields;
}

For order details you can write with this fook:

add_action('woocommerce_admin_order_data_after_billing_address', 'your_function', 10, 1);
function your_function{
echo '<p><strong>CIF:</strong> ' . get_post_meta($order->get_id(), 'billing_cif', true) . '</p>';}

And for write on emails, you can use this add_action hook:

'woocommerce_email_after_order_table'

Xiaoximena answered 10/11, 2021 at 15:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.