我想生成订购报告,订购产品包含特定名称。
例如我想要标题为“玩具”的产品的销售报告,产品包括“汽车玩具”,“工具玩具”,“自行车玩具”等。
我想使用Woocommerce WC_Admin_Report
类来生成报告,但我无法使用它。
include_once($woocommerce->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
$wc_report = new WC_Admin_Report();
$where_meta[] = array(
'type' => 'order_item_meta',
'meta_key' => '_product_name',
'operator' => 'LIKE',
'meta_value' => 'shirt'
);
$sold_products = $wc_report->get_order_report_data(array(
'data' => array(
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'_qty' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'quantity'
),
'_line_subtotal' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'gross'
),
'_line_total' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'gross_after_discount'
)
),
'query_type' => 'get_results',
'where_meta' => $where_meta,
'limit' => 20,
'order_status' => array( 'completed', 'processing')
));
where_meta
仅查找产品名称位于woocommerce_order_itemmeta
表中的woocommerce_order_items
表,那么我如何才能搜索woocommerce_order_items
?像这样的东西?
$where_meta[] = array(
'type' => 'order_item',
'meta_key' => 'order_item_name',
'operator' => 'LIKE',
'meta_value' => 'toy'
);
答案 0 :(得分:2)
你可以为$where_meta
这样做。
$products = new WP_Query( array(
's' => 'shirt',
'post_type' => 'product',
'fields' => 'ids'
) );
$product_ids = $products->posts;
$where_meta = array (
'relation' => 'OR',
array(
'type' => 'order_item_meta',
'meta_key' => array( '_product_id', '_variation_id' ),
'meta_value' => $product_ids,
'operator' => 'IN',
),
);
此外,您也可以这样做。更方便。
使用where
代替where_meta
。
$where => array(
array(
'key' => 'order_items.order_item_name',
'value' => '%shirt%',
'operator' => 'LIKE',
),
),
您还需要在init
上拨打此电话。那么它应该是这样的:
add_action('init', 'reports');
function reports() {
include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
$wc_report = new WC_Admin_Report();
$sold_products = $wc_report->get_order_report_data( array(
'data' => array(
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'_qty' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'quantity'
),
'_line_subtotal' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'gross'
),
'_line_total' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'gross_after_discount'
)
),
'where' => array(
array(
'key' => 'order_items.order_item_name',
'value' => '%shirt%',
'operator' => 'LIKE',
),
),
'group_by' => 'order_item_name',
'query_type' => 'get_results',
'limit' => 20,
'order_status' => array( 'completed', 'processing' ),
) );
print_r($sold_products);
}
不要忘记'group_by'
。
/* sample output of print_r($sold_products);
Array
(
[0] => stdClass Object
(
[product_id] => 70
[quantity] => 3
[gross] => 36
[gross_after_discount] => 36
)
[1] => stdClass Object
(
[product_id] => 53
[quantity] => 4
[gross] => 140
[gross_after_discount] => 140
)
[2] => stdClass Object
(
[product_id] => 50
[quantity] => 1
[gross] => 10
[gross_after_discount] => 10
)
)
*/