A script that will add a new report page for woocommerce reports. This snippet wil calculate gross sells by payment gateway.
include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 );
function my_custom_woocommerce_admin_reports( $reports ) {
$sales_by_country = array(
'sales_by_country' => array(
'title' => 'Sales By Payment gateway',
'description' => '',
'hide_title' => true,
'callback' => 'sales_by_payment_gateway',
),
);
// This can be: orders, customers, stock or taxes, based on where we want to insert our new reports page
$reports['orders']['reports'] = array_merge( $reports['orders']['reports'], $sales_by_country);
return $reports;
}
function sales_by_payment_gateway() {
$report = new WC_Report_Sales_By_Country();
$report->output_report();
}
class WC_Report_Sales_By_Country extends WC_Admin_Report {
/**
* Output the report.
*/
public function output_report() {
$ranges = array(
'year' => __( 'Year', 'woocommerce' ),
'last_month' => __( 'Last month', 'woocommerce' ),
'month' => __( 'This month', 'woocommerce' ),
);
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month';
if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) {
$current_range = 'month';
}
$this->check_current_range_nonce( $current_range );
$this->calculate_current_range( $current_range );
$hide_sidebar = true;
include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
}
/**
* Get the main chart.
*/
public function get_main_chart() {
global $wpdb;
$query_data = array(
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders',
'distinct' => true,
),
'_payment_method_title' => array(
'type' => 'meta',
'function' => '',
'name' => 'payment'
),
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'order_total'
),
);
$sales_by_country_orders = $this->get_order_report_data( array(
'data' => $query_data,
'query_type' => 'get_results',
'group_by' => 'payment',
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed' ),
'parent_order_status' => false,
) );
?>
<table class="widefat">
<thead>
<tr>
<th><strong>Payment gateway</strong></th>
<th><strong>Number Of Orders</strong></th>
<th><strong>Sales</strong></th>
</tr>
</thead>
<tbody>
<?php foreach( $sales_by_country_orders as $order ) {
?>
<tr>
<td><?php echo $order->payment; ?></td>
<td><?php echo $order->total_orders; ?></td>
<td><?php echo wc_price($order->order_total); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
}
2 reacties
This snippet works perfect, thanks! It only shows in the old Reports section of Woocommerce, can it be adapted for the new Woocommerce Analytics section?
Sorry for the late reply.
The new reports are very different of the old ones, But it should be possible. I haven’t looked into it myself. This page should give a clue how to achieve this: Extending WC admin reports