public function get_import_meta_fields()
 {
     $customer_detail = new WC_CRM_Customer();
     $customer_detail->init_general_fields();
     $customer_detail->init_address_fields();
     $fields = $customer_detail->general_fields;
     $billing = $customer_detail->billing_fields;
     $shipping = $customer_detail->shipping_fields;
     $keys = array('default' => __('Select fields to map', 'wc_crm'), 'wcrm_custom_meta' => __('Custom meta', 'wc_crm'), 'user_role' => __('Role', 'wc_crm'), 'user_login' => __('User Login', 'wc_crm'), 'user_group' => __('Group', 'wc_crm'), 'user_nicename' => __('Nicename', 'wc_crm'), 'display_name' => __('Display name', 'wc_crm'));
     if ($fields) {
         foreach ($fields as $key => $value) {
             if ($key == 'customer_brands') {
                 continue;
             }
             if ($key == 'customer_categories') {
                 continue;
             }
             $keys[$value['meta_key']] = $value['label'];
         }
     }
     if ($billing) {
         foreach ($billing as $key => $value) {
             $keys['billing_' . $key] = 'Billing ' . $value['label'];
         }
     }
     if ($shipping) {
         foreach ($shipping as $key => $value) {
             $keys['shipping_' . $key] = 'Shipping ' . $value['label'];
         }
     }
     return apply_filters('wcrm_get_import_meta_fields', $keys);
 }
 public static function save($customer_id, $new = false)
 {
     global $the_customer, $wpdb;
     $the_customer = new WC_CRM_Customer($customer_id);
     wc_crm_clear_notices();
     $data = array('status' => $_POST['customer_status']);
     $old_user_data = array();
     if ($the_customer->user_id > 0) {
         if (empty($_POST['user_email'])) {
             wc_crm_add_notice(__('Please enter an e-mail address.', 'wc_crm'), 'error');
         } elseif (!is_email($_POST['user_email'])) {
             wc_crm_add_notice(__("The email address isn't correct.", 'wc_crm'), 'error');
         } elseif (($owner_id = email_exists($_POST['user_email'])) && $owner_id != $the_customer->user_id) {
             wc_crm_add_notice(__("This email is already registered, please choose another one.", 'wc_crm'), 'error');
         }
         if (wc_crm_notice_count('error') > 0) {
             return;
         }
         $old_user_data = WP_User::get_data_by('id', $the_customer->user_id);
         $user_data_up = array('ID' => $the_customer->user_id, 'user_url' => $_POST['customer_site'], 'user_email' => $_POST['user_email']);
         wp_update_user($user_data_up);
         $the_customer->init_general_fields();
         $the_customer->init_address_fields();
         if ($the_customer->general_fields) {
             foreach ($the_customer->general_fields as $key => $field) {
                 if (!isset($field['type'])) {
                     $field['type'] = 'text';
                 }
                 if (!isset($field['meta_key'])) {
                     $field['meta_key'] = $key;
                 }
                 if (!isset($_POST[$key])) {
                     if ($field['type'] == 'multiselect') {
                         update_user_meta($the_customer->user_id, $field['meta_key'], array());
                     }
                     continue;
                 }
                 switch ($key) {
                     case "customer_twitter":
                         $post_value = str_replace('@', '', $_POST['customer_twitter']);
                         break;
                     default:
                         $post_value = $_POST[$key];
                         break;
                 }
                 update_user_meta($the_customer->user_id, $field['meta_key'], $post_value);
             }
         }
         if ($the_customer->billing_fields) {
             foreach ($the_customer->billing_fields as $key => $field) {
                 if (!isset($_POST['_billing_' . $key])) {
                     continue;
                 }
                 $post_value = $_POST['_billing_' . $key];
                 update_user_meta($the_customer->user_id, 'billing_' . $key, $post_value);
             }
         }
         if ($the_customer->shipping_fields) {
             foreach ($the_customer->shipping_fields as $key => $field) {
                 if (!isset($_POST['_shipping_' . $key])) {
                     continue;
                 }
                 $post_value = $_POST['_shipping_' . $key];
                 update_user_meta($the_customer->user_id, 'shipping_' . $key, $post_value);
             }
         }
         update_user_meta($the_customer->user_id, 'preferred_payment_method', $_POST['_payment_method']);
         update_user_meta($the_customer->user_id, 'payment_method', $_POST['_payment_method']);
         $group_ids = array();
         if (isset($_POST['wc_crm_customer_groups'])) {
             $group_ids = $_POST['wc_crm_customer_groups'];
         }
         $the_customer->update_groups($group_ids);
         if (isset($_POST['account_name'])) {
             $account_id = $_POST['account_name'];
             add_post_meta($account_id, '_wc_crm_customer_id', $customer_id);
         }
         $data = array('email' => $_POST['user_email'], 'first_name' => $_POST['first_name'], 'last_name' => $_POST['last_name'], 'state' => $_POST['_billing_state'], 'city' => $_POST['_billing_city'], 'country' => $_POST['_billing_country'], 'status' => $_POST['customer_status']);
         $res = $wpdb->update("{$wpdb->prefix}wc_crm_customer_list", $data, array('c_id' => $the_customer->customer_id));
         do_action('profile_update', $the_customer->user_id, $old_user_data);
         // update the post (may even be a revision / autosave preview)
         do_action('acf/save_post', 'user_' . $the_customer->user_id);
         do_action('acf/save_post', 'user_' . $the_customer->user_id);
         do_action('wc_crm_save_customer', $the_customer->customer_id, $the_customer, $old_user_data);
     } else {
         if ($the_customer->customer_id > 0) {
             $the_customer->init_general_fields();
             $disabled = array('first_name', 'last_name', 'user_email', 'customer_status');
             if ($the_customer->general_fields) {
                 foreach ($the_customer->general_fields as $key => $field) {
                     if (in_array($key, $disabled)) {
                         continue;
                     }
                     if (!isset($field['type'])) {
                         $field['type'] = 'text';
                     }
                     if (!isset($field['meta_key'])) {
                         $field['meta_key'] = $key;
                     }
                     if (!isset($_POST[$key])) {
                         if ($field['type'] == 'multiselect') {
                             wc_crm_update_cmeta($the_customer->customer_id, $field['meta_key'], array());
                         }
                         continue;
                     }
                     switch ($key) {
                         case "customer_twitter":
                             $post_value = str_replace('@', '', $_POST['customer_twitter']);
                             break;
                         default:
                             $post_value = $_POST[$key];
                             break;
                     }
                     wc_crm_update_cmeta($the_customer->customer_id, $field['meta_key'], $post_value);
                 }
             }
             if (isset($_POST['account_name'])) {
                 $account_id = $_POST['account_name'];
                 add_post_meta($account_id, '_wc_crm_customer_email', $customer_id);
             }
             $res = $wpdb->update("{$wpdb->prefix}wc_crm_customer_list", $data, array('c_id' => $the_customer->customer_id));
             do_action('guest_update', $the_customer->customer_id, $the_customer->email);
         }
     }
     if ($new === false) {
         wc_crm_add_notice(__("Customer updated.", 'wc_crm'), 'success');
     }
 }
        public function select_customer_id()
        {
            if (isset($_GET['post_type']) && $_GET['post_type'] == 'shop_order' && isset($_GET['c_id']) && !empty($_GET['c_id'])) {
                $c_id = $_GET['c_id'];
                $the_customer = new WC_CRM_Customer($c_id);
                ob_start();
                if ($the_customer->user_id > 0) {
                    $c_name = $the_customer->first_name . ' ' . $the_customer->last_name . ' (#' . $the_customer->user_id . ' - ' . $the_customer->user_email . ')';
                    ?>
          jQuery("#customer_user").select2("data", {"id":"<?php 
                    echo $the_customer->user_id;
                    ?>
","text":"<?php 
                    echo $c_name;
                    ?>
"});
          <?php 
                }
                $the_customer->init_address_fields();
                $formatted_shipping_address = wp_kses($the_customer->get_formatted_shipping_address(), array("br" => array()));
                $formatted_billing_address = wp_kses($the_customer->get_formatted_billing_address(), array("br" => array()));
                $__b_address = $the_customer->billing_fields;
                $__s_address = $the_customer->shipping_fields;
                foreach ($__b_address as $key => $field) {
                    $var_name = 'billing_' . $key;
                    ?>
            jQuery('#_billing_<?php 
                    echo $key;
                    ?>
').val( "<?php 
                    echo addslashes($the_customer->{$var_name});
                    ?>
" );
            <?php 
                }
                foreach ($__s_address as $key => $field) {
                    $var_name = 'shipping_' . $key;
                    ?>
            jQuery('#_shipping_<?php 
                    echo $key;
                    ?>
').val( "<?php 
                    echo addslashes($the_customer->{$var_name});
                    ?>
" );
            <?php 
                }
                ?>
            jQuery('.order_data_column_container .order_data_column').last().find('.address')
            .html("<?php 
                echo "<p><strong>" . __("Address", "woocommerce") . ":</strong>" . addslashes($formatted_shipping_address) . "</p>";
                ?>
");

            jQuery('.order_data_column_container .order_data_column').first().next().find('.address')
            .html("<?php 
                echo "<p><strong>" . __("Address", "woocommerce") . ":</strong>" . addslashes($formatted_billing_address) . "</p>";
                ?>
");

            jQuery('.js_field-country').trigger('change');
        <?php 
                $js_string = ob_get_contents();
                ob_end_clean();
                wc_enqueue_js($js_string);
            }
        }