在Woocommerce订单编辑页面中,编辑客户订单不会更新Wordpress用户数据中的客户用户详细信息。
编辑订单中的运输和账单信息后,是否可以自动保存客户详细信息?
是的,
可以使用所有帐单和运输客户字段(包括"帐单电子邮件"和"帐单电话"字段(的钩子save_post_shop_order
钩子来完成此操作:
add_action('save_post_shop_order', 'update_wp_user_data', 50, 3 );
function update_wp_user_data( $post_id, $post, $update ) {
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
update_post_meta( $post_id, '_invoices_files', 'auto-save-verif' );
return $post_id;
}
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) ){
update_post_meta( $post_id, '_invoices_files', 'user-roles-verif' );
return $post_id;
}
// Get the customer ID and check if it's valid
$customer_id = get_post_meta( $post_id, '_customer_user', true );
if( empty($customer_id) || $customer_id == 0 )
return $post_id;
// Set all field keys in arrays
$field_keys = array('first_name', 'last_name', 'company', 'address_1', 'address_2', 'city',
'postcode', 'country', 'state');
$fields_keys2 = array('email', 'phone');
foreach( $field_keys as $key ){
if( isset($_POST['_billing_'.$key]) ){
update_user_meta( $customer_id, 'billing_'.$key, sanitize_text_field( $_POST['_billing_'.$key] ) );
}
if( isset($_POST['_shipping_'.$key]) ){
update_user_meta( $customer_id, 'shipping_'.$key, sanitize_text_field( $_POST['_shipping_'.$key] ) );
}
}
foreach( $fields_keys2 as $key ){
if( isset($_POST['_billing_'.$key]) ){
update_user_meta( $customer_id, 'billing_'.$key, sanitize_text_field( $_POST['_billing_'.$key] ) );
}
}
}
代码进入函数.php活动子主题(或活动主题(的文件。经过测试并工作。