在 Woocommerce 中编辑订单详细信息时自动更新 Wordpress 用户数据
Auto update Wordpress user data when editing their order details in Woocommerce
在 Woocommerce 订单编辑页面中,编辑客户订单不会更新 Wordpress 用户数据中的客户用户详细信息。
是否可以在编辑订单中的运输和账单信息后自动保存客户详细信息?
是的,这可以使用 save_post_shop_order
钩子来完成,用于所有开票和送货客户字段(包括 "billing email" 和 "billing phone" 字段):
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] ) );
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
在 Woocommerce 订单编辑页面中,编辑客户订单不会更新 Wordpress 用户数据中的客户用户详细信息。
是否可以在编辑订单中的运输和账单信息后自动保存客户详细信息?
是的,这可以使用 save_post_shop_order
钩子来完成,用于所有开票和送货客户字段(包括 "billing email" 和 "billing phone" 字段):
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] ) );
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。