2016-11-23 61 views
2

我需要根据数据范围获取每个产品销售报告。这意味着我将输入产品ID(或ID)以及开始日期和结束日期,该功能将在此(开始日期和结束日期)内返回该产品的销售数量。所以我尝试了WC_Admin_ReportWC_Report_Sales_By_Product。我试图代码是 -WooCommerce管理报告:根据日期范围获取定制产品报告

function the_dramatist_get_report(){ 

    include_once(WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-admin-report.php'); 
    include_once(WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-report-sales-by-product.php'); 

    $reports = new WC_Report_Sales_By_Product(); 
    $reports->start_date = strtotime('2016-11-11'); 
    $reports->end_date = strtotime('2016-11-22'); 

    $reports->product_ids = 15; 

    $total_items = absint($reports->get_order_report_data(array(
     'data' => array(
      '_qty' => array(
       'type'   => 'order_item_meta', 
       'order_item_type' => 'line_item', 
       'function'  => 'SUM', 
       'name'   => 'order_item_count' 
      ) 
     ), 
     'where_meta' => array(
      'relation' => 'OR', 
      array(
       'type'  => 'order_item_meta', 
       'meta_key' => array('_product_id', '_variation_id'), 
       'meta_value' => $reports->product_ids, 
       'operator' => 'IN' 
      ) 
     ), 
     'query_type' => 'get_var', 
     'filter_range' => true 
    ))); 
    return $total_items; 
} 

但上面的代码返回0时,我已经测试了,它应该是1。所以如果你帮我解决这个问题会更好。

如果您有任何其他想法完成此任务,请随时分享。

回答

0

尝试了很多次后,我还没有让这个类实例工作。所以我遵循最古老的CRUD技术来解决这个问题(其实这个课程也在做),我写了我自己的SQL查询。这WC_Admin_Report类也是这样做的。这有点像WooCommerce的查询构建器。顺便下面SQL代码解决了我的问题 -

SELECT order_item_meta__product_id.meta_value AS product_id, 
     Sum(order_item_meta__qty.meta_value) AS order_item_count 
FROM wp_posts AS posts 
     INNER JOIN wp_woocommerce_order_items AS order_items 
       ON posts.id = order_items.order_id 
     INNER JOIN wp_woocommerce_order_itemmeta AS order_item_meta__qty 
       ON (order_items.order_item_id = 
        order_item_meta__qty.order_item_id) 
        AND (order_item_meta__qty.meta_key = '_qty') 
     INNER JOIN wp_woocommerce_order_itemmeta AS order_item_meta__product_id 
       ON (order_items.order_item_id = order_item_meta__product_id.order_item_id) 
        AND (order_item_meta__product_id.meta_key = '_product_id') 
     INNER JOIN wp_woocommerce_order_itemmeta AS 
        order_item_meta__product_id_array 
       ON order_items.order_item_id = order_item_meta__product_id_array.order_item_id 
WHERE posts.post_type IN ('shop_order', 'shop_order_refund') 
     AND posts.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold') 
     AND posts.post_date >= '2016-11-15' 
     AND posts.post_date < '2016-11-24' 
     AND ((order_item_meta__product_id_array.meta_key IN ('_product_id', '_variation_id') 
     AND order_item_meta__product_id_array.meta_value IN ('15'))) 
GROUP BY product_id 

希望也能帮到你。

2

WordPress中的WooCommerce Order被视为Post。所以订单日期是 存储在wp_posts.post_datewp_posts.post_type = shop_order

因此,首先获取特定日期范围内的所有订单,然后检查这些订单中的订单项中的product_id。

下面是代码:

function getproductReportbyDateRange($start_date, $end_date, $product_ids = []) 
{ 
    $totalProduct = []; 
    foreach ($product_ids as $product_id) 
    { 
     $totalProduct[$product_id] = 0; 
    } 
    unset($product_id); 
    $args = array(
     'post_type' => 'shop_order', //WooCommerce Order Status. 
     'post_status' => array('wc-completed'), //list of WooCommerce order status. 
     'posts_per_page' => -1, 
     'date_query' => array(
      array(
       'after' => $start_date, 
       'before' => $end_date, 
       'inclusive' => true, 
      ), 
     ), 
    ); 
    $query = new WP_Query($args); 
    //print_r($query->posts); 
    //die; 
    if (isset($query->posts)) 
    { 
     foreach ($query->posts as $post) 
     { 
      $order = new WC_Order($post->ID); 
      $items = $order->get_items(); 
      foreach ($items as $item) 
      { 
       //$product_name = $item['name']; 
       $product_id = $item['product_id']; 
       //$product_variation_id = $item['variation_id']; 
       if (in_array($product_id, $product_ids)) 
       { 
        $totalProduct[$product_id] = $totalProduct[$product_id] + $item['qty']; 
       } 
      } 
     } 
    } 
    return $totalProduct; 
} 

此代码放在你的活跃儿童主题(或主题)的function.php文件或也以任何插件文件。

用法

要获得一个单品的产品数量

getproductReportbyDateRange('2016-11-11','2016-11-22',[37]); 

要得到产品的计数的多个产品

getproductReportbyDateRange('2016-11-11','2016-11-22',[37, 53]); 

该代码已经过测试并且功能齐全。


参考:

+0

你会再次测试这个代码吗?由于看到我可以说有很多错误。在'foreach($ items为$ item)'后,你调用'$ product_id = $ item ['product_id'];'它会通过一个错误。之后,您检查并增加了'$ ctr'。但是,任何人都可以购买多个。那么会发生什么?所以请再次查看您的代码。 @Raunak Gupta –

+0

@the_dramatist:是的,我测试了它正常工作的代码,没有错误。是的,我忘记了'qty',我已经更新了我的答案。 –

+0

@the_dramatist:你有没有试过我的答案? –