Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce中编辑订单详细信息时自动更新Wordpress用户数据_Php_Wordpress_Woocommerce_Orders_User Data - Fatal编程技术网

Php 在Woocommerce中编辑订单详细信息时自动更新Wordpress用户数据

Php 在Woocommerce中编辑订单详细信息时自动更新Wordpress用户数据,php,wordpress,woocommerce,orders,user-data,Php,Wordpress,Woocommerce,Orders,User Data,在Woocommerce订单编辑页面中,编辑客户订单不会更新Wordpress用户数据中的客户用户详细信息 编辑订单中的发货和账单信息后,是否可以自动保存客户详细信息?是,可以使用保存post\u shop\u订单钩子为所有发货和账单客户字段(包括“账单电子邮件”和“账单电话”字段)执行此操作: 代码进入活动子主题(或活动主题)的function.php文件。测试和工作 add_action('save_post_shop_order', 'update_wp_user_data', 50, 3

在Woocommerce订单编辑页面中,编辑客户订单不会更新Wordpress用户数据中的客户用户详细信息


编辑订单中的发货和账单信息后,是否可以自动保存客户详细信息?

是,可以使用
保存post\u shop\u订单
钩子为所有发货和账单客户字段(包括“账单电子邮件”和“账单电话”字段)执行此操作:

代码进入活动子主题(或活动主题)的function.php文件。测试和工作

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] ) );
        }
    }
}