Ejemplo n.º 1
0
function wc_customer_relationship_manager_bulk_handler()
{
    $group = '';
    if (isset($_REQUEST['group']) && !empty($_REQUEST['group']) && $_REQUEST['group'] > 0) {
        $group = '&group=' . $_REQUEST['group'];
    }
    if (!isset($_REQUEST['action']) && !isset($_REQUEST['action2'])) {
        return;
    }
    $action = '';
    $statuses = wc_crm_get_statuses_slug();
    if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) {
        $action = $_REQUEST['action'];
    } elseif (isset($_REQUEST['action2']) && -1 != $_REQUEST['action2']) {
        $action = $_REQUEST['action2'];
    }
    if (!empty($action)) {
        if (array_key_exists($action, $statuses)) {
            if (!isset($_REQUEST['user_id']) || empty($_REQUEST['user_id'])) {
                return;
            }
            $user_ids = $_REQUEST['user_id'];
            wc_crm_change_customer_status($action, $user_ids);
        } else {
            if (strstr($action, 'crm_add_to_group_')) {
                if ((!isset($_REQUEST['user_id']) || empty($_REQUEST['user_id'])) && (!isset($_REQUEST['order_id']) || empty($_REQUEST['order_id']))) {
                    return;
                }
                $filer = '';
                if (!empty($_REQUEST['user_id'])) {
                    $filer .= "user_id IN(" . implode(',', $_REQUEST['user_id']) . ")";
                }
                if (!empty($_REQUEST['order_id'])) {
                    if (!empty($filer)) {
                        $filer .= ' OR ';
                    }
                    $filer .= "order_id IN(" . implode(',', $_REQUEST['order_id']) . ")";
                }
                $group_id = substr($action, strlen('crm_add_to_group_'));
                global $wpdb;
                $cutomers = $wpdb->get_results("SELECT email FROM {$wpdb->prefix}wc_crm_customer_list WHERE {$filer}");
                if (!empty($cutomers)) {
                    $user_emails = array();
                    foreach ($cutomers as $cutomer) {
                        $user_emails[] = $cutomer->email;
                    }
                    wc_crm_add_to_group($group_id, $user_emails);
                    $_REQUEST['report_action'] = "added_to_group";
                }
            } else {
                return;
            }
        }
        wp_redirect(admin_url() . 'admin.php?page=wc-customer-relationship-manager' . $group);
    }
}
 public function customer_process_bulk_action()
 {
     $wp_list_table = new WP_List_Table();
     $action = $wp_list_table->current_action();
     if ($action === false) {
         return;
     }
     switch ($action) {
         case 'email':
             if (!empty($_REQUEST['customer_id'])) {
                 $ids = array();
                 foreach ($_REQUEST['customer_id'] as $key => $c_id) {
                     $ids[] = 'c_id[' . $key . ']=' . $c_id;
                 }
                 wp_redirect('admin.php?page=wc_crm&screen=email&' . implode('&', $ids));
             }
             break;
         case 'export_csv':
             $ids = array();
             if (!empty($_REQUEST['customer_id'])) {
                 $ids = $_REQUEST['customer_id'];
             }
             WC_CRM_Export::init($ids);
             #wp_redirect( 'admin.php?page=wc_crm&screen=email&'.implode('&', $ids) );
             break;
         default:
             global $wpdb;
             if (strrpos($action, 'mark_as_') === 0) {
                 if (!empty($_REQUEST['customer_id'])) {
                     $ids = $_REQUEST['customer_id'];
                     $statuses = wc_crm_get_statuses_slug();
                     $status = substr($action, 8);
                     foreach ($ids as $c_id) {
                         $user_id = $wpdb->get_var("SELECT user_id FROM {$wpdb->prefix}wc_crm_customer_list WHERE c_id = {$c_id} LIMIT 1 ");
                         if (array_key_exists($status, $statuses) && $user_id > 0) {
                             update_user_meta($user_id, 'customer_status', $status);
                         }
                     }
                     if (array_key_exists($status, $statuses)) {
                         global $wpdb;
                         $sql = "UPDATE {$wpdb->prefix}wc_crm_customer_list\n                        SET status = '{$status}'\n                        WHERE c_id IN (" . implode(',', $ids) . ")\n              ";
                         $wpdb->query($sql);
                         wc_crm_add_notice(__('Customer status updated.', 'wc_crm'), 'success');
                     }
                 }
             } else {
                 if (strrpos($action, 'crm_add_to_group_') === 0) {
                     $ids = $_REQUEST['customer_id'];
                     if (count($ids) > 0) {
                         $group_id = substr($action, strlen('crm_add_to_group_'));
                         wc_crm_add_to_group($group_id, $ids);
                         $count = count($ids);
                         wc_crm_add_notice(sprintf(_n('%d customer added to the group.', '%d customers added to the group.', 'wc_crm'), $count), 'success');
                     }
                 }
             }
             wc_crm_clear_transient();
             break;
     }
 }