【问题标题】:WooCommerce Admin Reports : Get custom product report based on date rangeWooCommerce 管理员报告:根据日期范围获取自定义产品报告
【发布时间】:2017-04-07 00:18:22
【问题描述】:

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

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;
}

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

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

【问题讨论】:

    标签: php mysql wordpress woocommerce hook-woocommerce


    【解决方案1】:

    WordPress 中的 WooCommerce 订单被视为发布。所以订单日期是 存储在wp_posts.post_date 中,wp_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]);
    

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


    参考:

    【讨论】:

    • 你能再测试一下这段代码吗?因为看到我可以说有很多错误。在foreach ($items as $item) 你打电话给$product_id = $item['product_id']; 之后,它会报错。之后,您检查并增加了$ctr。但是在一个订单中,任何人都可以购买多个。那时会发生什么?因此,请再次检查您的代码。 @Raunak Gupta
    • @the_dramatist:是的,我已经测试了它工作正常的代码,没有错误。是的,我忘记了qty,我已经更新了我的答案。
    【解决方案2】:

    在尝试了很多次之后,我还没有让那个类实例工作。所以我遵循最古老的 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 
    

    希望对你也有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-13
      • 2023-03-28
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多