我需要根据数据范围获取每个产品销售报告。这意味着我将输入产品id(或多个id)以及开始日期和结束日期,函数将返回我该产品在此(开始日期和结束日期)时间段内的销售编号。所以我试着WC_Admin_Report
和WC_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;
}
但上面的代码正在返回
0
当我已经测试过它应该是
1
. 所以如果你能帮我解决这个问题就更好了。
最合适的回答,由SO网友:CodeMascot 整理而成
经过多次尝试,我还没有使该类实例正常工作。所以我遵循了最古老的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
希望这对你也有帮助。