Editable admin custom billing fields error issue i

2019-07-25 21:36发布

I have an error located in this code (that adds in order edit pages editable custom billing fields):

add_filter( 'woocommerce_admin_billing_fields' , 'order_admin_custom_fields' );
function order_admin_custom_fields( $fields ) {
    global $theorder;

    $fields['billing_address_3'] = array(
        'label' => __( 'Home', 'woocommerce' ),
        'value'=> get_post_meta( $theorder->get_id(), 'Home', true ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['billing_address_4'] = array(
        'label' => __( 'Entrance', 'woocommerce' ),
        'value'=> get_post_meta( $theorder->get_id(), 'Entrance', true ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['billing_address_5'] = array(
        'label' => __( 'Floor', 'woocommerce' ),
        'value'=> get_post_meta( $theorder->get_id(), 'Floor', true ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    return $fields;
}

The guilty line: 'value'=> get_post_meta( $theorder->get_id(), 'Home', true ),

Report on the error:

example.com [Wed Jul 04 02:36:28 2018] [error] [pid 148187] sapi_apache2.c(362): [client 37.146.123.6:33708] PHP Fatal error:  Uncaught Error: Call to a member function get_id() on null in /home/c/cb36070/example.com/public_html/wp-content/themes/theme-name/functions.php:607\nStack trace:\n#0 /home/c/cb36070/example.com/public_html/wp-includes/class-wp-hook.php(286): order_admin_custom_fields(Array)\n#1 /home/c/cb36070/example.com/public_html/wp-includes/plugin.php(203): WP_Hook->apply_filters(Array, Array)\n#2 /home/c/cb36070/example.com/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php(87): apply_filters('woocommerce_adm...', Array)\n#3 /home/c/cb36070/example.com/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php(526): WC_Meta_Box_Order_Data::init_address_fields()\n#4 /home/c/cb36070/example.com/public_html/wp-includes/class-wp-hook.php(286): WC_Meta_Box_Order_Data::save(951, Object(WP_Post))\n#5 /home/c/cb36070/example.com/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)\n#6 /home/c/cb360 in /home/c/cb36070/example.com/public_html/wp-content/themes/theme-name/functions.php on line 607

But I need to get the saved values for those custom billing fields.

How can I solve this error issue, and get the custom billing field values?

The added (saved) meta data is made with this code:

add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['billing_address_3'] ) ) {
        update_post_meta( $order_id, 'Home', sanitize_text_field( $_POST['billing_address_3'] ) );
    }

    if ( ! empty( $_POST['billing_address_4'] ) ) {
        update_post_meta( $order_id, 'Entrance', sanitize_text_field( $_POST['billing_address_4'] ) );
    }

    if ( ! empty( $_POST['billing_address_5'] ) ) {
        update_post_meta( $order_id, 'Floor', sanitize_text_field( $_POST['billing_address_5'] ) );
    }
}

Any help is appreciated.

1条回答
乱世女痞
2楼-- · 2019-07-25 21:54

The problem is that $theorder is not defined and you can't use get_id() method on it.

But the main problem in your code comes from your checkout billing fields first. They should be set, displayed and saved as follow (taking a particular care of the meta_keys to be used when data is saved):

// Frontend: Display the custom billing fields (in checkout and my account)
add_filter( 'woocommerce_billing_fields' ,'add_custom_billing_fields', 20, 1 );
function add_custom_billing_fields( $fields ) {

    $fields['billing_address_3'] = array(
        'label' => __( 'Home', 'woocommerce' ),
        'placeholder'   => _x('Fill in your Home', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    $fields['billing_address_4'] = array(
        'label' => __( 'Entrance', 'woocommerce' ),
        'placeholder'   => _x('Fill in your Entrance', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    $fields['billing_address_5'] = array(
        'label' => __( 'Floor', 'woocommerce' ),
        'placeholder'   => _x('Fill in your Floor', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
    );

    return $fields;
}

// Save the custom billing fields (once order is placed)
add_action( 'woocommerce_checkout_create_order', 'save_custom_billingt_fields', 20, 2 );
function save_custom_billingt_fields( $order, $data ) {
    if ( isset( $_POST['billing_address_3'] ) && ! empty( $_POST['billing_address_3'] ) ) {
        $order->update_meta_data('_billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) );
        update_user_meta( $order->get_customer_id(), 'billing_address_3', sanitize_text_field( $_POST['billing_address_3'] ) );
    }
    if ( isset( $_POST['billing_address_4'] ) && ! empty( $_POST['billing_address_4'] ) ) {
        $order->update_meta_data('_billing_address_4', sanitize_text_field( $_POST['billing_address_4'] ) );
        update_user_meta( $order->get_customer_id(), 'billing_address_4', sanitize_text_field( $_POST['billing_address_4'] ) );
    }
    if ( isset( $_POST['billing_address_5'] ) && ! empty( $_POST['billing_address_5'] ) ) {
        $order->update_meta_data('_billing_address_5', sanitize_text_field( $_POST['billing_address_5'] ) );
        update_user_meta( $order->get_customer_id(), 'billing_address_5', sanitize_text_field( $_POST['billing_address_5'] ) );
    }
}

You will see that those custom fields are also in My account > addresses > Edit billing address. And everything related is auto sync. No need of additional validation or saving code...

Now in admin order pages, for woocommerce_admin_billing_fields admin hook, the 'value' key doesn't exist in the field array and it is the guilty.

As you have correctly set and saved your custom checkout fields, you don't need any 'value' array key, as the data will be auto populated, if it exist. So your code will be:

// Backend: Display editable custom billing fields
add_filter( 'woocommerce_admin_billing_fields' , 'order_admin_custom_fields' );
function order_admin_custom_fields( $fields ) {
    global $the_order;

    $fields['address_3'] = array(
        'label' => __( 'Home', 'woocommerce' ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['address_4'] = array(
        'label' => __( 'Entrance', 'woocommerce' ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    $fields['address_5'] = array(
        'label' => __( 'Floor', 'woocommerce' ),
        'show'  => true,
        'wrapper_class' => 'form-field-wide',
        'style' => '',
    );

    return $fields;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

The error is now definitively gone

查看更多
登录 后发表回答