Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

woocommerce_checkout_update_order_meta action is not working

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.

like image 670
Aitazaz Khan Avatar asked May 18 '17 20:05

Aitazaz Khan


1 Answers

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.

like image 98
LoicTheAztec Avatar answered Nov 16 '22 01:11

LoicTheAztec