wp_postmeta db table

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..

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.

Read more here: woocommerce_checkout_update_order_meta action is not working


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: