Reputation: 1687
I'm trying to write PHP code to list top selling items (with quantity) and top selling categories (with total sales amount) in a tabular form in descending order.
Item | Quantity
Category | Sales
I know we can retrieve this from Reports screen in WooCommerce, but I need to get this values so I can display them else where in the WordPress admin panel in a widget. I know how to create dashboard widgets, by the way.
Does anyone know how to implement this? Any help is appreciated.
Thanks.
Upvotes: 3
Views: 1928
Reputation: 65264
you should look into woocommerce files and look for WC_Admin_Report
. You can see some examples and work your way with what you want.
I'll give you this example I made.
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();
$data = $wc_report->get_order_report_data( array(
'data' => array(
'_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'
),
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'order_item_name' => array(
'type' => 'order_item',
'function' => '',
'name' => 'order_item_name',
),
),
'group_by' => 'product_id',
'order_by' => 'quantity DESC',
'query_type' => 'get_results',
'limit' => 20,
'order_status' => array( 'completed', 'processing' ),
) );
print_r($data);
}
sample output of print_r($data);
Array
(
[0] => stdClass Object
(
[quantity] => 4
[gross] => 140
[product_id] => 53
[order_item_name] => Happy Ninja
)
[1] => stdClass Object
(
[quantity] => 3
[gross] => 36
[product_id] => 70
[order_item_name] => Flying Ninja
)
[2] => stdClass Object
(
[quantity] => 1
[gross] => 10
[product_id] => 50
[order_item_name] => Patient Ninja
)
)
you have the data and that would get you up and running for Item | Quantity
.
Upvotes: 2