woocommerce_checkout_update_order_meta action is not working

Viewed 17712

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.

2 Answers

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'

Related