Example #1
0
 function woo_ce_return_count($dataset)
 {
     global $wpdb;
     $count_sql = null;
     switch ($dataset) {
         /* WooCommerce */
         case 'products':
             $post_type = 'product';
             $count = wp_count_posts($post_type);
             break;
         case 'categories':
             $term_taxonomy = 'product_cat';
             $count = wp_count_terms($term_taxonomy);
             break;
         case 'tags':
             $term_taxonomy = 'product_tag';
             $count = wp_count_terms($term_taxonomy);
             break;
         case 'orders':
             $post_type = 'shop_order';
             $count = wp_count_posts($post_type);
             $exclude_post_types = array('auto-draft');
             if (woo_ce_count_object($count, $exclude_post_types) > 100) {
                 $count = '~' . woo_ce_count_object($count, $exclude_post_types) . ' *';
             } else {
                 $count = woo_ce_count_object($count, $exclude_post_types);
             }
             break;
         case 'customers':
             $post_type = 'shop_order';
             $count = wp_count_posts($post_type);
             $exclude_post_types = array('auto-draft');
             if (woo_ce_count_object($count, $exclude_post_types) > 100) {
                 $count = '~' . woo_ce_count_object($count, $exclude_post_types) . ' *';
             } else {
                 $count = 0;
                 $args = array('post_type' => $post_type, 'numberposts' => -1, 'post_status' => woo_ce_post_statuses(), 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'field' => 'slug', 'terms' => array('pending', 'on-hold', 'processing', 'completed'))));
                 $orders = get_posts($args);
                 if ($orders) {
                     $customers = array();
                     foreach ($orders as $order) {
                         $order->email = get_post_meta($order->ID, '_billing_email', true);
                         if (empty($order->email)) {
                             if ($order->user_id = get_post_meta($order->ID, '_customer_user', true)) {
                                 $user = get_userdata($order->user_id);
                                 if ($user) {
                                     $order->email = $user->user_email;
                                 }
                                 unset($user);
                             } else {
                                 $order->email = '-';
                             }
                         }
                         if (!in_array($order->email, $customers)) {
                             $customers[$order->ID] = $order->email;
                             $count++;
                         }
                     }
                 }
             }
             break;
         case 'coupons':
             $post_type = 'shop_coupon';
             $count = wp_count_posts($post_type);
             break;
     }
     if (isset($count) || $count_sql) {
         if (isset($count)) {
             $count = woo_ce_count_object($count);
             return $count;
         } else {
             if ($count_sql) {
                 $count = $wpdb->get_var($count_sql);
             } else {
                 $count = 0;
             }
         }
         return $count;
     } else {
         return 0;
     }
 }
Example #2
0
 function woo_ce_return_count($dataset)
 {
     global $wpdb;
     $count_sql = null;
     switch ($dataset) {
         case 'products':
             $post_type = 'product';
             $args = array('post_type' => $post_type, 'posts_per_page' => 1);
             $query = new WP_Query($args);
             $count = $query->found_posts;
             break;
         case 'categories':
             $term_taxonomy = 'product_cat';
             $count = wp_count_terms($term_taxonomy);
             break;
         case 'tags':
             $term_taxonomy = 'product_tag';
             $count = wp_count_terms($term_taxonomy);
             break;
         case 'orders':
             $post_type = 'shop_order';
             $args = array('post_type' => $post_type, 'posts_per_page' => 1);
             $query = new WP_Query($args);
             $count = $query->found_posts;
             break;
         case 'customers':
             $post_type = 'shop_order';
             $args = array('post_type' => $post_type, 'posts_per_page' => -1, 'post_status' => woo_ce_post_statuses(), 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'field' => 'slug', 'terms' => array('pending', 'on-hold', 'processing', 'completed'))), 'fields' => 'ids');
             $query = new WP_Query($args);
             $count = $query->found_posts;
             if ($count > 100) {
                 $count = sprintf('~%s *', $count);
             } else {
                 $customers = array();
                 if ($query->have_posts()) {
                     while ($query->have_posts()) {
                         $query->the_post();
                         $email = get_post_meta(get_the_ID(), '_billing_email', true);
                         if (!in_array($email, $customers)) {
                             $customers[get_the_ID()] = $email;
                         }
                         unset($email);
                     }
                     $count = count($customers);
                 }
                 wp_reset_postdata();
             }
             /*
             				if( false ) {
             					$orders = get_posts( $args );
             					if( $orders ) {
             						$customers = array()	;
             						foreach( $orders as $order ) {
             							$order->email = get_post_meta( $order->ID, '_billing_email', true );
             							if( empty( $order->email ) ) {
             								if( $order->user_id = get_post_meta( $order->ID, '_customer_user', true ) ) {
             									$user = get_userdata( $order->user_id );
             									if( $user )
             										$order->email = $user->user_email;
             									unset( $user );
             								} else {
             									$order->email = '-';
             								}
             							}
             							if( !in_array( $order->email, $customers ) ) {
             								$customers[$order->ID] = $order->email;
             								$count++;
             							}
             						}
             						unset( $orders, $order );
             					}
             				}
             */
             break;
         case 'coupons':
             $post_type = 'shop_coupon';
             $count = wp_count_posts($post_type);
             break;
     }
     if (isset($count) || $count_sql) {
         if (isset($count)) {
             $count = woo_ce_count_object($count);
             return $count;
         } else {
             if ($count_sql) {
                 $count = $wpdb->get_var($count_sql);
             } else {
                 $count = 0;
             }
         }
         return $count;
     } else {
         return 0;
     }
 }