此代码中有一个错误(该错误在订单编辑页面中添加了可编辑的自定义结算字段):
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;
}
内line:'value'=> get_post_meta( $theorder->get_id(), 'Home', true ),
报告错误:
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
但是我需要获取那些自定义计费字段的保存值。
如何解决此错误问题,并获取自定义结算字段值?
添加(保存)的元数据是使用以下代码完成的:
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'] ) );
}
}
感谢您的帮助。
答案 0 :(得分:1)
问题是$theorder
未定义,因此无法对其使用get_id()
方法。
但是代码中的主要问题首先来自您的结帐帐单字段。应该按照设置,显示和保存它们(特别注意保存数据时要使用的meta_keys
):
// 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'] ) );
}
}
您将看到这些自定义字段也在“我的帐户”>“地址”>“编辑帐单地址”中。并且所有相关的都是自动同步。无需其他验证或保存代码...
现在在管理订单页面中,对于woocommerce_admin_billing_fields
管理员挂钩,'value'
键在字段数组中不存在,这是有罪的。
正确设置并保存了自定义结帐字段后,您将不需要任何'value'
数组键,因为数据将自动填充(如果存在)。因此您的代码将是:
// 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;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
该错误现在已经消失