Exemplo n.º 1
0
 /**
  * Get customers with a term: all, newsletters_site, newsletters_site_partners, date(between 2 dates) or if order is higher than.
  * @param string $term
  * @param price/date $dt1 may be null
  * @param date $dt2 may be null
  * @return array
  */
 function get_customers($term, $dt1 = null, $dt2 = null)
 {
     global $wpdb;
     $users;
     switch ($term) {
         case 'users_all':
             $query = $wpdb->prepare("SELECT ID AS USER_ID, '' AS POST_ID FROM {$wpdb->users} WHERE %d", 1);
             $list_users = $wpdb->get_results($query, OBJECT);
             break;
         case 'customers_all':
             $query = $wpdb->prepare("SELECT us.ID AS USER_ID, GROUP_CONCAT( ps.ID ) AS POST_ID FROM {$wpdb->users} us JOIN {$wpdb->posts} ps ON us.ID = ps.post_author AND ps.post_type = %s GROUP BY USER_ID", 'wpshop_shop_order');
             $list_users = $wpdb->get_results($query, OBJECT);
             break;
         case 'newsletters_site':
             $query = $wpdb->prepare("SELECT user_id AS USER_ID, GROUP_CONCAT( ID ) AS POST_ID FROM {$wpdb->usermeta} JOIN {$wpdb->posts} ON post_author = user_id AND post_type = %s WHERE meta_key = %s AND ( meta_value LIKE ('%%%s%%') || meta_value LIKE ('%%%s%%') ) GROUP BY USER_ID", 'wpshop_shop_order', 'user_preferences', 's:16:"newsletters_site";i:1;', 's:16:"newsletters_site";b:1;');
             $list_users = $wpdb->get_results($query, OBJECT);
             break;
         case 'newsletters_site_partners':
             $query = $wpdb->prepare("SELECT user_id AS USER_ID, GROUP_CONCAT( ID ) AS POST_ID FROM {$wpdb->usermeta} JOIN {$wpdb->posts} ON post_author = user_id AND post_type = %s WHERE meta_key = %s AND ( meta_value LIKE ('%%%s%%') || meta_value LIKE ('%%%s%%') ) GROUP BY USER_ID", 'wpshop_shop_order', 'user_preferences', 's:25:"newsletters_site_partners";i:1;', 's:25:"newsletters_site_partners";b:1;');
             $list_users = $wpdb->get_results($query, OBJECT);
             break;
         case 'date':
             $query = $wpdb->prepare("SELECT us.`ID` AS USER_ID, GROUP_CONCAT( ps.`ID` ) AS POST_ID FROM {$wpdb->users} us JOIN {$wpdb->posts} ps ON `post_author` = us.`ID` AND `post_type` = %s WHERE `user_registered` >= %s AND `user_registered` <= %s GROUP BY USER_ID", 'wpshop_shop_order', date("Y-m-j", strtotime($dt1)), date("Y-m-j", strtotime("+1 day", strtotime($dt2))));
             $list_users = $wpdb->get_results($query, OBJECT);
             break;
         case 'orders':
             $query = $wpdb->prepare("SELECT us.ID AS USER_ID, GROUP_CONCAT( ps.ID ) AS POST_ID FROM {$wpdb->users} us JOIN {$wpdb->posts} ps ON us.ID = ps.post_author AND ps.post_type = %s GROUP BY USER_ID", 'wpshop_shop_order');
             $list_users = $wpdb->get_results($query, OBJECT);
             break;
     }
     $users_array = array();
     $users_array[] = array('name' => __('Name', 'wps_export'), 'first_name' => __('First name', 'wps_export'), 'email' => __('Mail', 'wps_export'), 'tel' => __('Phone', 'wps_export'), 'registered' => __('Registered', 'wps_export'));
     if (!empty($list_users)) {
         $billing_address_indicator = get_option('wpshop_billing_address');
         $billing_address_indicator = $billing_address_indicator['choice'];
         foreach ($list_users as $user_post) {
             if ($term == 'orders') {
                 $vuser = false;
             }
             $user = get_userdata($user_post->USER_ID);
             $tmp_array = array();
             $last_name = get_user_meta($user->ID, 'last_name', true);
             $first_name = get_user_meta($user->ID, 'first_name', true);
             if (empty($last_name)) {
                 $last_name = $user->display_name;
             }
             if (empty($first_name)) {
                 $first_name = '-';
             }
             $tmp_array['name'] = $last_name;
             $tmp_array['first_name'] = $first_name;
             $tmp_array['email'] = $user->user_email;
             $tmp_array['tel'] = '';
             $result = wps_address::get_addresses_list($user->ID);
             if (!empty($result) && !empty($result[$billing_address_indicator])) {
                 foreach ($result[$billing_address_indicator] as $address_id => $address_data) {
                     if (!empty($address_data['phone'])) {
                         $tmp_array['tel'] = $address_data['phone'];
                     }
                 }
             }
             $tmp_array['registered'] = date('d M Y H:i', strtotime($user->user_registered));
             $posts_id = explode(',', $user_post->POST_ID);
             $orders = get_posts(array('include' => $posts_id, 'post_type' => 'wpshop_shop_order', 'posts_per_page' => -1));
             foreach ($orders as $order) {
                 if ($term == 'orders') {
                     $command = get_post_meta($order->ID, '_order_postmeta', true);
                     if (!empty($dt1) && !empty($command['order_grand_total']) && $command['order_grand_total'] >= $dt1 || !empty($dt2) && $dt2 === true && $command['order_payment']['customer_choice']['method'] == 'free') {
                         $vuser = true;
                     }
                 }
             }
             if ($term != 'orders') {
                 $users_array[] = $tmp_array;
             } elseif ($vuser) {
                 $users_array[] = $tmp_array;
             }
         }
     }
     return $users_array;
 }
 /**
  * AJAX - Choose customer to create order
  */
 function wps_order_choose_customer()
 {
     $status = false;
     $billing_data = $shipping_data = '';
     $customer_id = !empty($_POST['customer_id']) ? intval($_POST['customer_id']) : null;
     if (!empty($customer_id)) {
         $wps_address = new wps_address();
         $billing_option = get_option('wpshop_billing_address');
         $shipping_option = get_option('wpshop_shipping_address_choice');
         $billing_option = $billing_option['choice'];
         $customer_addresses_list = wps_address::get_addresses_list($customer_id);
         $status = true;
         $billing_data = '<div class="wps-alert-info">' . sprintf(__('No Billing address created, <a href="%s" title="' . __('Create a new billing address', 'wpshop') . '" class="thickbox">create one</a>', 'wpshop'), admin_url('admin-ajax.php') . '?action=wps-add-an-address-in-admin&address_type=' . $billing_option . '&customer_id=' . $customer_id . '&height=600') . '</div>';
         if (!empty($shipping_option) && !empty($shipping_option['activate'])) {
             $shipping_option = $shipping_option['choice'];
             $shipping_data = '<div class="wps-alert-info">' . sprintf(__('No shipping address created, <a href="%s" title="' . __('Create a new shipping address', 'wpshop') . '" class="thickbox">create one</a>', 'wpshop'), admin_url('admin-ajax.php') . '?action=wps-add-an-address-in-admin&address_type=' . $shipping_option . '&customer_id=' . $customer_id . '&height=600') . '</div>';
         }
         if (!empty($customer_addresses_list)) {
             foreach ($customer_addresses_list as $address_type => $customer_addresses) {
                 if ($billing_option == $address_type) {
                     $billing_data = $wps_address->display_address_in_administration($customer_addresses, $address_type);
                 } else {
                     $shipping_data = $wps_address->display_address_in_administration($customer_addresses, $address_type);
                 }
             }
         }
     }
     echo json_encode(array('status' => $status, 'billing_data' => $billing_data, 'shipping_data' => $shipping_data));
     wp_die();
 }
 /**
  * AJAX - Valid Checkout Step three
  */
 function wps_checkout_valid_step_three()
 {
     $response = '';
     $status = true;
     $shipping_address = !empty($_POST['shipping_address_id']) ? wpshop_tools::varSanitizer($_POST['shipping_address_id']) : null;
     $billing_address = !empty($_POST['billing_address_id']) ? wpshop_tools::varSanitizer($_POST['billing_address_id']) : null;
     $user_id = get_current_user_id();
     $response = '<div class="wps-alert-error"><ul>';
     if ($user_id != 0) {
         $shipping_option = get_option('wpshop_shipping_address_choice');
         $billing_option = get_option('wpshop_billing_address');
         $user_addresses = wps_address::get_addresses_list($user_id);
         if (!empty($shipping_option) && !empty($shipping_option['activate'])) {
             /** Check Shipping address **/
             if (empty($shipping_address)) {
                 $status = false;
                 /** Check if user have already create a shipping address **/
                 if (!empty($shipping_option['choice']) && !empty($user_addresses) && !empty($user_addresses[$shipping_option['choice']])) {
                     $response .= '<li>' . __('You must select a shipping address', 'wpshop') . '</li>';
                 } else {
                     $response .= '<li>' . __('You must create a shipping address', 'wpshop') . '</li>';
                 }
             }
         }
         /** Check Billing address **/
         if (empty($billing_address)) {
             $status = false;
             if (!empty($billing_option['choice']) && !empty($user_addresses) && !empty($user_addresses[$billing_option['choice']])) {
                 $response .= '<li>' . __('You must select a billing address', 'wpshop') . '</li>';
             } else {
                 $response .= '<li>' . __('You must create a billing address', 'wpshop') . '</li>';
             }
         }
     } else {
         $status = false;
         $response .= '<li>' . __('You must be logged to pass to next step', 'wpshop') . '</li>';
     }
     $response .= '</ul></div>';
     /** If no error **/
     if ($status) {
         $_SESSION['shipping_address'] = $shipping_address;
         $_SESSION['billing_address'] = $billing_address;
         $permalink_option = get_option('permalink_structure');
         $checkout_page_id = wpshop_tools::get_page_id(get_option('wpshop_checkout_page_id'));
         /** Checking if no shipping method is required and it is a quotation or a free order **/
         $shipping_option = get_option('wps_shipping_mode');
         $available_shipping_method = false;
         if (!empty($shipping_option) && !empty($shipping_option['modes'])) {
             foreach ($shipping_option['modes'] as $shipping_mode_id => $shipping_mode) {
                 if (!empty($shipping_mode['active']) && $shipping_mode['active'] == 'on') {
                     $available_shipping_method = true;
                 }
             }
         }
         if (!$available_shipping_method) {
             $_SESSION['shipping_method'] = 'No Shipping method required';
             $order_id = !empty($_SESSION['cart']['order_id']) ? wpshop_tools::varSanitizer($_SESSION['cart']['order_id']) : 0;
             if (!empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['cart_type']) && $_SESSION['cart']['cart_type'] == 'quotation') {
                 $status = true;
                 $payment_method = $_SESSION['payment_method'] = 'quotation';
                 $order_id = wpshop_checkout::process_checkout($payment_method, $order_id, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address']);
                 $response = get_permalink(wpshop_tools::get_page_id($checkout_page_id)) . (!empty($permalink_option) ? '?' : '&') . 'order_step=6';
             } elseif (!empty($_SESSION) && !empty($_SESSION['cart']) && isset($_SESSION['cart']['order_amount_to_pay_now']) && number_format($_SESSION['cart']['order_amount_to_pay_now'], 2, '.', '') == '0.00') {
                 $status = true;
                 $payment_method = $_SESSION['payment_method'] = 'free';
                 $order_id = wpshop_checkout::process_checkout($payment_method, $order_id, get_current_user_id(), $_SESSION['billing_address'], $_SESSION['shipping_address']);
                 $permalink_option = get_option('permalink_structure');
                 $checkout_page_id = wpshop_tools::get_page_id(get_option('wpshop_checkout_page_id'));
                 $url = get_permalink($checkout_page_id) . (!empty($permalink_option) ? '?' : '&') . 'order_step=6';
                 // 						wpshop_tools::wpshop_safe_redirect( $url );
                 $response = $url;
             } else {
                 $status = true;
                 $response = get_permalink(wpshop_tools::get_page_id($checkout_page_id)) . (!empty($permalink_option) ? '?' : '&') . 'order_step=5';
             }
         } else {
             $status = true;
             $response = get_permalink($checkout_page_id) . (!empty($permalink_option) ? '?' : '&') . 'order_step=4';
         }
     }
     //Stock checking verification
     $this->checking_stock();
     echo json_encode(array('status' => $status, 'response' => $response));
     die;
 }