/** * Get customer detail */ function get_customer_detail() { global $thepostid, $post, $woocommerce; $b_country = ''; $s_country = ''; if ($this->user_id) { $b_country = get_user_meta($this->user_id, 'billing_country', true); $s_country = get_user_meta($this->user_id, 'shipping_country', true); } else { if ($this->order_id) { $b_country = $this->order->billing_country; $s_country = $this->order->shipping_country; $thepostid = $this->order_id; } } if (!$this->order_id) { $this->init_general_fields($this->user_id); $thepostid = true; } $this->init_address_fields($b_country, $s_country); ?> <div class="panel-wrap woocommerce" id="customer_data"> <div id="order_data" class="panel"> <?php echo get_avatar($this->user_id, 100); ?> <?php if ($this->user_id) { ?> <h2><?php echo __('Customer', 'wc_customer_relationship_manager') . ' #' . $this->user_id . ' '; _e('Details', 'wc_customer_relationship_manager'); ?> </h2> <?php } else { if ($this->order_id) { ?> <h2><?php _e('Customer Details', 'wc_customer_relationship_manager'); ?> </h2> <?php } } ?> <?php if ($this->user_id) { $user_data = get_userdata(intval($this->user_id)); ?> <p class="order_number total_value" style="float: left; margin-right: 10px;"> <?php _e('Value:', 'wc_customer_relationship_manager'); ?> <?php $identifier = get_option('woocommerce_crm_unique_identifier'); if ($identifier == 'username_email') { $total_spent = wc_crm_get_order_value($this->user_id); $num_orders = wc_crm_get_num_orders($this->user_id); } else { $email = get_user_meta($this->user_id, 'billing_email', true); $num_orders = wc_crm_get_num_orders($email, '_billing_email', true); $total_spent = wc_crm_get_order_value($email, '_billing_email', true); } echo wc_price($total_spent); ?> </p> <p class="order_number num_orders" style="float: left; margin-right: 10px;"> <?php _e('Orders:', 'wc_customer_relationship_manager'); ?> <?php echo sprintf(_n('1 order', '%s orders', $num_orders, 'wc_customer_relationship_manager'), $num_orders); ?> </p> <p class="order_number created_date" style="float: left; margin-right: 10px;"> <?php _e('Created:', 'wc_customer_relationship_manager'); ?> <?php echo wc_crm_get_customer_pretty_time($user_data->user_registered); ?> </p> <?php } else { if ($this->order_id) { ?> <?php } } ?> <div class="order_data_column_container"> <?php if (!$this->order_id) { ?> <div class="order_data_column"> <h4><?php _e('General Details', 'wc_customer_relationship_manager'); ?> </h4> <div id="customer_general_details"> <?php if ($this->general_fields) { foreach ($this->general_fields as $key => $field) { $field['id'] = $key; if (!isset($field['type'])) { $field['type'] = 'text'; } if ($key == 'customer_status' && empty($field['value'])) { $default_status = get_option('wc_crm_default_status_crm'); $field['value'] = !empty($default_status) ? $default_status : 'Customer'; } switch ($field['type']) { case "select": woocommerce_wp_select($field); break; case "multiselect": wc_crm_wp_multiselect($field); break; default: woocommerce_wp_text_input($field); break; } } } ?> </div> </div> <?php } else { global $wpdb; $ID = (int) $this->order_id; $user_email = $wpdb->get_var("SELECT email FROM {$wpdb->prefix}wc_crm_customer_list WHERE order_id = {$ID} LIMIT 1"); ?> <div class="order_data_column" style=" float: none;"> <p class="form-field form-field-wide"><label for="account_name"><?php _e('Account Name:', 'wc_customer_relationship_manager'); ?> </label> <select id="account_name" name="account_name" class="chosen_select" data-allow_clear="true" data-placeholder="<?php _e('Select an account', 'wc_customer_relationship_manager'); ?> "> <option value=""></option> <?php $selected = wc_crm_get_customer_account($user_email); $accounts = wc_crm_get_accounts(); foreach ($accounts as $account) { echo '<option value="' . esc_attr($account->ID) . '" ' . selected($account->ID, $selected, false) . '>' . esc_html($account->post_title) . '</option>'; } ?> </select> </p> </div> <div class="clear"></div> <?php } ?> <div class="order_data_column" id="order_data_column_billing"> <h4><?php _e('Billing Details ', 'wc_customer_relationship_manager'); ?> <a class="edit_address" href="#" <?php echo empty($this->user_id) ? 'style="display: none;"' : ''; ?> ><img src="<?php echo WC()->plugin_url(); ?> /assets/images/icons/edit.png" alt="Edit" width="14" /></a></h4> <?php if ($this->user_id || $this->order_id) { // Display values echo '<div class="address">'; if ($this->get_formatted_billing_address()) { echo '<p><strong>' . __('Address', 'wc_customer_relationship_manager') . ':</strong>' . wp_kses($this->get_formatted_billing_address(), array('br' => array())) . '</p>'; } else { echo '<p class="none_set"><strong>' . __('Address', 'wc_customer_relationship_manager') . ':</strong> ' . __('No billing address set.', 'wc_customer_relationship_manager') . '</p>'; } foreach ($this->billing_fields as $key => $field) { if (isset($field['show']) && $field['show'] === false) { continue; } $field_name = 'billing_' . $key; if ($this->user_id) { $field_value = get_user_meta($this->user_id, $field_name, true); } elseif ($this->order_id) { $name_var = 'billing_' . $key; $field_value = $this->order->{$field_name}; } if (!empty($field_value)) { echo '<p><strong>' . esc_html($field['label']) . ':</strong> ' . make_clickable(esc_html($field_value)) . '</p>'; } } if (WC()->payment_gateways()) { $payment_gateways = WC()->payment_gateways->payment_gateways(); } if ($this->user_id) { $payment_method = get_user_meta($this->user_id, 'preferred_payment_method', true); } else { $payment_method = !empty($this->order->payment_method) ? $this->order->payment_method : ''; } if ($payment_method) { echo '<p><strong>' . __('Preferred Payment Method', 'wc_customer_relationship_manager') . ':</strong> ' . (isset($payment_gateways[$payment_method]) ? esc_html($payment_gateways[$payment_method]->get_title()) : esc_html($payment_method)) . '</p>'; } echo '</div>'; } if (!$this->order_id) { // Display form echo '<div class="edit_address" ' . (!$this->user_id ? 'style="display: block;"' : '') . '>'; foreach ($this->billing_fields as $key => $field) { if (!isset($field['type'])) { $field['type'] = 'text'; } if (isset($data['_billing_' . $key]) && $data['_billing_' . $key]) { $value = $data['_billing_' . $key]; } elseif ($this->user_id) { $value = get_user_meta($this->user_id, 'billing_' . $key, true); } elseif ($this->order_id) { $var_name = 'billing_' . $key; $value = $this->order->{$var_name}; } else { $value = ''; } $post = new WC_Order($this->order_id); $class_ = ''; if (!empty($field['class']) && is_array($field['class'])) { $class_ = implode(' ', $field['class']); } switch ($field['type']) { case "select": woocommerce_wp_select(array('id' => '_billing_' . $key, 'class' => $class_, 'label' => $field['label'], 'options' => $field['options'], 'value' => $value)); break; default: woocommerce_wp_text_input(array('id' => '_billing_' . $key, 'class' => $class_, 'label' => $field['label'], 'value' => $value)); break; } } ?> <p class="form-field form-field-wide"> <label><?php _e('Payment Method:', 'wc_customer_relationship_manager'); ?> </label> <select name="_payment_method" id="_payment_method" class="first"> <option value=""><?php _e('N/A', 'wc_customer_relationship_manager'); ?> </option> <?php $found_method = false; foreach ($payment_gateways as $gateway) { if ($gateway->enabled == "yes") { echo '<option value="' . esc_attr($gateway->id) . '" ' . selected($payment_method, $gateway->id, false) . '>' . esc_html($gateway->get_title()) . '</option>'; if ($payment_method == $gateway->id) { $found_method = true; } } } if (!$found_method && !empty($payment_method)) { echo '<option value="' . esc_attr($payment_method) . '" selected="selected">' . __('Other', 'wc_customer_relationship_manager') . '</option>'; } else { echo '<option value="other">' . __('Other', 'wc_customer_relationship_manager') . '</option>'; } ?> </select> </p> <?php echo '</div>'; } ?> </div> <div class="order_data_column" id="order_data_column_shipping"> <h4><?php _e('Shipping Details', 'wc_customer_relationship_manager'); ?> <a class="edit_address" href="#" <?php echo empty($this->user_id) ? 'style="display: none;"' : ''; ?> ><img src="<?php echo WC()->plugin_url(); ?> /assets/images/icons/edit.png" alt="Edit" width="14" /></a></h4> <?php if ($this->user_id || $this->order_id) { // Display values echo '<div class="address">'; if ($this->get_formatted_shipping_address()) { echo '<p><strong>' . __('Address', 'wc_customer_relationship_manager') . ':</strong>' . wp_kses($this->get_formatted_shipping_address(), array('br' => array())) . '</p>'; } else { echo '<p class="none_set"><strong>' . __('Address', 'wc_customer_relationship_manager') . ':</strong> ' . __('No shipping address set.', 'wc_customer_relationship_manager') . '</p>'; } if ($this->shipping_fields) { foreach ($this->shipping_fields as $key => $field) { if (isset($field['show']) && $field['show'] === false) { continue; } $field_name = 'shipping_' . $key; if ($this->user_id) { $field_value = get_user_meta($this->user_id, $field_name, true); } elseif ($this->order_id) { $name_var = 'billing_' . $key; $field_value = $this->order->{$field_name}; } if (!empty($order->{$field_name})) { echo '<p><strong>' . esc_html($field['label']) . ':</strong> ' . make_clickable(esc_html($field_value)) . '</p>'; } } } echo '</div>'; } if (!$this->order_id) { // Display form echo '<div class="edit_address" ' . (empty($this->user_id) ? 'style="display: block;"' : '') . '><p><button class="button" id="copy-billing-same-as-shipping" type="button">' . __('Copy from billing', 'wc_customer_relationship_manager') . '</button></p>'; if ($this->shipping_fields) { foreach ($this->shipping_fields as $key => $field) { if (!isset($field['type'])) { $field['type'] = 'text'; } if (isset($data['copy']) && $data['copy']) { if (isset($data['_billing_' . $key]) && $data['_billing_' . $key]) { $value = $data['_billing_' . $key]; } elseif ($this->user_id) { $value = get_user_meta($this->user_id, 'billing_' . $key, true); } elseif ($this->order_id) { $var_name = 'billing_' . $key; $value = $this->order->{$var_name}; } else { $value = ''; } } elseif (isset($data['_shipping_' . $key]) && $data['_shipping_' . $key]) { $value = $data['_shipping_' . $key]; } elseif ($this->user_id) { $value = get_user_meta($this->user_id, 'shipping_' . $key, true); } elseif ($this->order_id) { $var_name = 'shipping_' . $key; $value = $this->order->{$var_name}; } else { $value = ''; } $class_ = ''; if (!empty($field['class']) && is_array($field['class'])) { $class_ = implode(' ', $field['class']); } switch ($field['type']) { case "select": woocommerce_wp_select(array('id' => '_shipping_' . $key, 'class' => $class_, 'label' => $field['label'], 'options' => $field['options'], 'value' => $value)); break; default: woocommerce_wp_text_input(array('id' => '_shipping_' . $key, 'class' => $class_, 'label' => $field['label'], 'value' => $value)); break; } } } echo '</div>'; } ?> </div> </div> <?php if ($this->user_id || $this->order_id) { $address_l = str_replace('<br/>', ', ', $this->get_formatted_billing_address(true)); ?> <div id="customer_address_map_canvas" > <div class="acf-google-map active" data-zoom="14" data-zoom="14" data-lng="144.96328" data-lat="-37.81411" data-id="map-crm" > <div style="display:none;"> <div style="display:none;"> <input class="input-address" type="hidden" value="<?php echo $address_l; ?> " > <input class="input-lat" type="hidden" value="" > <input class="input-lng" type="hidden" value="" > </div> </div> <div class="title" style="display:none;"> <div class="no-value"> <a title="Find current location" class="acf-sprite-locate ir" href="#">Locate</a> <input type="text" class="search" placeholder="Search for address..." value="<?php echo $address_l; ?> "> </div> </div> <div class="canvas" style="height: 300px"></div> </div> </div> <script> var wc_pos_customer_formatted_billing_address = '<?php echo $address_l; ?> '; </script> <?php } ?> <div class="clear"></div> </div> </div> <script> jQuery('.form-row-left').parent().css('float', 'left'); jQuery('.form-row-right').parent().css('float', 'right'); </script> <?php }
/** * Handle CSV file download */ function export_csv() { header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename=customers_' . date('Y-m-d') . '.csv'); header('Pragma: no-cache'); $__wc_crm_customer_details = new WC_Crm_Customer_Details(0, 0); $__wc_crm_customer_details->init_address_fields('', '', false); $__b_address = $__wc_crm_customer_details->billing_fields; $__s_address = $__wc_crm_customer_details->shipping_fields; $o = WC_CRM()->orders(); $data = $o->get_orders(); echo '"Customer name",'; foreach ($__b_address as $key => $label) { if ($key == 'first_name' || $key == 'last_name') { continue; } echo '"Billing ' . $label['label'] . '",'; } foreach ($__s_address as $key => $label) { if ($key == 'first_name' || $key == 'last_name') { continue; } echo '"Shipping ' . $label['label'] . '",'; } echo '"Username",'; echo '"Last purchase date",'; echo '"Number of orders",'; echo '"Total value",'; echo "\"Subscribed\"\n"; if (woocommerce_crm_mailchimp_enabled()) { $members = woocommerce_crm_get_members(); } foreach ($data as $item) { //$item = get_object_vars ( $customer ); if ($item['user_id']) { $user_id = $item['user_id']; $wc_crm_customer_details = new WC_Crm_Customer_Details($user_id, 0); $wc_crm_customer_details->init_address_fields('', '', false); $b_address = $wc_crm_customer_details->billing_fields; $s_address = $wc_crm_customer_details->shipping_fields; $data = get_user_meta($item['user_id']); echo '"' . $data['first_name'][0] . ' ' . $data['last_name'][0] . '",'; foreach ($b_address as $key => $value) { if ($key == 'first_name' || $key == 'last_name') { continue; } if ($key == 'country') { echo '"' . $item['country'] . '",'; continue; } if ($key == 'email') { echo '"' . $item['email'] . '",'; continue; } $field_name = 'billing_' . $key; $field_value = get_user_meta($user_id, $field_name, true); echo '"' . $field_value . '",'; } foreach ($s_address as $key => $value) { if ($key == 'first_name' || $key == 'last_name') { continue; } $field_name = 'shipping_' . $key; $field_value = get_user_meta($user_id, $field_name, true); echo '"' . $field_value . '",'; } $user = @get_userdata($user_id); echo '"' . (isset($user->user_login) ? $user->user_login : __('Guest', 'wc_customer_relationship_manager')) . '",'; $item['num_orders'] = wc_crm_get_num_orders($item['user_id']); $item['total_spent'] = wc_crm_get_order_value($item['user_id']); } else { $order_id = $item['order_id']; $user_id = 0; $order = new WC_Order($order_id); $wc_crm_customer_details_g = new WC_Crm_Customer_Details(0, $order_id); $wc_crm_customer_details_g->init_address_fields('', '', false); $b_address = $wc_crm_customer_details_g->billing_fields; $s_address = $wc_crm_customer_details_g->shipping_fields; $first_name = get_post_meta($item['order_id'], '_billing_first_name', true); $last_name = get_post_meta($item['order_id'], '_billing_last_name', true); echo '"' . $first_name . ' ' . $last_name . '",'; foreach ($b_address as $key => $value) { if ($key == 'first_name' || $key == 'last_name') { continue; } if ($key == 'country') { echo '"' . $item['country'] . '",'; continue; } if ($key == 'email') { echo '"' . $item['email'] . '",'; continue; } $name_var = 'billing_' . $key; $field_value = $wc_crm_customer_details_g->order->{$name_var}; echo '"' . $field_value . '",'; } foreach ($s_address as $key => $value) { if ($key == 'first_name' || $key == 'last_name') { continue; } $var_name = 'shipping_' . $key; $field_value = $wc_crm_customer_details_g->order->{$name_var}; echo '"' . $field_value . '",'; } echo '"' . __('Guest', 'wc_customer_relationship_manager') . '",'; $item['num_orders'] = wc_crm_get_num_orders($item['email'], '_billing_email', true); $item['total_spent'] = wc_crm_get_order_value($item['email'], '_billing_email', true); } $total_spent = wc_crm_price_num_decimals($item['total_spent']); $last_purchase = $item['order_id'] ? woocommerce_crm_get_pretty_time($item['order_id'], true) : ''; echo '"' . $last_purchase . '",'; echo '"' . $item['num_orders'] . '",'; if (woocommerce_crm_mailchimp_enabled()) { $enrolled_plain = in_array($item['email'], $members) ? 'yes' : 'no'; echo '"' . $total_spent . '",'; echo '"' . $enrolled_plain . "\"\n"; } else { echo '"' . $total_spent . "\"\n"; } } }
function column_order_value($item) { $num_orders = 0; $total_spent = 0; $identifier = get_option('woocommerce_crm_unique_identifier'); if (isset($item['order_value'])) { $total_spent = $item['order_value']; } else { if ($item['user_id'] && !empty($item['user_id']) && get_userdata($item['user_id'])) { $meta = get_user_meta($item['user_id']); if ($identifier == 'username_email') { $total_spent = wc_crm_get_order_value($item['user_id']); } else { if ($meta && isset($meta['billing_email'])) { $email = $meta['billing_email'][0]; $total_spent = wc_crm_get_order_value($email, '_billing_email', true); } } } else { if ($item['order_id'] && !empty($item['order_id'])) { $email = get_post_meta($item['order_id'], '_billing_email', true); $total_spent = wc_crm_get_order_value($email, '_billing_email', true); } } } if (isset($item['num_orders'])) { $num_orders = (int) $item['num_orders']; } else { if ($item['user_id'] && !empty($item['user_id']) && get_userdata($item['user_id'])) { $meta = get_user_meta($item['user_id']); if ($identifier == 'username_email') { $num_orders = wc_crm_get_num_orders($item['user_id']); } else { if ($meta && isset($meta['billing_email'])) { $email = $meta['billing_email'][0]; $num_orders = wc_crm_get_num_orders($email, '_billing_email', true); } } } else { if ($item['order_id'] && !empty($item['order_id'])) { $email = get_post_meta($item['order_id'], '_billing_email', true); $num_orders = wc_crm_get_num_orders($email, '_billing_email', true); } } } $num_orders = $num_orders > 0 ? '<br><small class="meta">' . sprintf(_n('%d order', '%d orders', $num_orders, 'wc_customer_relationship_manager'), $num_orders) . '</small>' : ''; return wc_price($total_spent) . $num_orders; }