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.
The problem is that
$theorder
is not defined and you can't useget_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):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:Code goes in function.php file of your active child theme (or active theme). Tested and works.