我需要根据数据范围获取每个产品销售报告。这意味着我将输入产品ID(或ID)以及开始日期和结束日期,该功能将在此(开始日期和结束日期)内返回该产品的销售数量。所以我尝试了WC_Admin_Report
和WC_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
。所以如果你帮我解决这个问题会更好。
如果您有任何其他想法完成此任务,请随时分享。
你会再次测试这个代码吗?由于看到我可以说有很多错误。在'foreach($ items为$ item)'后,你调用'$ product_id = $ item ['product_id'];'它会通过一个错误。之后,您检查并增加了'$ ctr'。但是,任何人都可以购买多个。那么会发生什么?所以请再次查看您的代码。 @Raunak Gupta –
@the_dramatist:是的,我测试了它正常工作的代码,没有错误。是的,我忘记了'qty',我已经更新了我的答案。 –
@the_dramatist:你有没有试过我的答案? –