示例#1
0
 /**
  * Sum of orders for a specific product
  *
  * @param array $product_ids
  * @param array $args (optional)
  *
  * @return object
  */
 public static function sum_orders_for_products(array $product_ids, array $args = array())
 {
     global $wpdb;
     $dates = PV_Queries::orders_within_range();
     $defaults = array('status' => apply_filters('wc_product_vendor_completed_statuses', array('completed', 'processing')), 'dates' => array('before' => $dates['before'], 'after' => $dates['after']));
     foreach ($product_ids as $id) {
         $posts = get_posts(array('numberposts' => -1, 'post_type' => 'product_variation', 'post_parent' => $id));
         if (!empty($posts)) {
             foreach ($posts as $post) {
                 $product_ids[] = $post->ID;
             }
         }
     }
     $args = wp_parse_args($args, $defaults);
     $sql = "\n\t\t\tSELECT COUNT(order_id) as total_orders,\n\t\t\t       SUM(total_due + total_shipping + tax) as line_total,\n\t\t\t       SUM(qty) as qty,\n\t\t\t       product_id\n\n\t\t\tFROM {$wpdb->prefix}pv_commission\n\n\t\t\tWHERE   product_id IN ('" . implode("','", $product_ids) . "')\n\t\t\tAND     time >= '" . $args['dates']['after'] . "'\n\t\t\tAND     time <= '" . $args['dates']['before'] . "'\n\t\t\tAND     status != 'reversed'\n\t\t";
     if (!empty($args['vendor_id'])) {
         $sql .= "\n\t\t\t\tAND vendor_id = {$args['vendor_id']}\n\t\t\t";
     }
     $sql .= "\n\t\t\tGROUP BY product_id\n\t\t\tORDER BY time DESC;\n\t\t";
     $orders = $wpdb->get_results($sql);
     return $orders;
 }