How to Get WooCommerce Current Month Sales Summary in the Dashboard Widget
- May 16, 2017
- Leave a comment
WordPress provides a very pleasant feature of Dashboard Widget to keep the user updated with their activities. In this article, you will learn how to make a Widget that consists of WooCommerce current month archive of complete orders.
You need to add the following code snippets in your Theme’s functions.php file. As an outcome, it will generate the sales summary of current month on your Dashboard.
1 2 3 4 5 6 7 8 9 10 11 |
/** * Monthly Sale Summary Dashboard Widget */ function pt_woocommerce_monthly_sales_widget() { wp_add_dashboard_widget( 'ptmp-monthly-sales-summary', // Widget slug. __('Monthly Sales Summary', 'theme-domain'), // Title. 'ptmp_montly_sales_dashboard_widget' // Display function. ); } add_action('wp_dashboard_setup', 'pt_woocommerce_monthly_sales_widget'); |
After creating the Widget, add ptmp_montly_sales_dashboard_widget() function. As a result, this function will list the sales history of each product sold along with their line total.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/** * Sales history of products with their line total. */ function ptmp_montly_sales_dashboard_widget() { global $woocommerce, $wpdb, $product; include_once($woocommerce->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php'); // WooCommerce Admin Report $wc_report = new WC_Admin_Report(); // Set date parameters for the current month $start_date = strtotime(date('Y-m', current_time('timestamp')) . '-01 midnight'); $end_date = strtotime('+1month', $start_date) - 86400; $wc_report->start_date = $start_date; $wc_report->end_date = $end_date; // Avoid max join size error $wpdb->query('SET SQL_BIG_SELECTS=1'); // Get data for current month sold products $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', 'group_by' => 'product_id', 'where_meta' => '', 'order_by' => 'quantity DESC', 'order_types' => wc_get_order_types('order_count'), 'filter_range' => TRUE, 'order_status' => array('completed'), )); // List Sales Items if (!empty($sold_products)) { ?> <div class="ptmp-products-list"> <table class="ptmp-wp-list-table widefat striped"> <thead> <tr> <th class="manage-column column-name" scope="col"><strong><?php _e('Name', 'text-domain') ?></strong></th> <th class="manage-column column-count" scope="col"><strong><?php _e('Items', 'text-domain') ?></strong></th> <th class="manage-column column-earnings num" scope="col"><strong><?php _e('Sales', 'text-domain') ?></strong></th> </tr> </thead> <tbody > <?php foreach ($sold_products as $product) { ?> <tr> <td class="seller-this-month"><a href="<?php echo esc_url(get_edit_post_link(intval($product->product_id))); ?>"><strong><?php echo html_entity_decode(get_the_title($product->product_id)); ?></strong></a></td> <td><?php echo intval($product->quantity); ?></td> <?php $price = $product->gross; $product_price = wc_price($price); ?> <td><?php echo $product_price; ?></td> </tr> <?php } ?> </tbody> </table> </div> <?php } else { echo '<p>' . __( 'Currently, there is no sale for this month.', 'text-domain') . '</p>'; } } |
For the CSS of Dashboard Widget, you need to create a CSS file to include it in the admin area using the following code:
1 2 3 4 5 |
function load_custom_wp_admin_style() { wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/css/admin-style.css', false, '1.0.0' ); wp_enqueue_style( 'custom_wp_admin_css' ); } add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' ); |
Place the CSS in admin-style.css file for a widget look and feel using the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#ptmp-monthly-sales-summary .ptmp-products-list a { display: block; color: #aaa; -webkit-transition: all ease .5s; position: relative; font-size: 12px; } #ptmp-monthly-sales-summary .ptmp-products-list a:before { font-family: WooCommerce; speak: none; font-weight: 400; font-variant: normal; text-transform: none; -webkit-font-smoothing: antialiased; margin: 0 12px 12px 0; text-indent: 0; top: 0; left: 0; height: 100%; text-align: center; content: ""; font-size: 2em; position: relative; width: auto; color: #464646; float: left; } #ptmp-monthly-sales-summary .ptmp-products-list td{ padding-top: 15px; padding-bottom: 5px; } #ptmp-monthly-sales-summary .ptmp-products-list td.seller-this-month a:before { content: "\e006"; } #ptmp-monthly-sales-summary .ptmp-products-list a:hover strong, #ptmp-monthly-sales-summary .ptmp-products-list a:hover:before { color: #2ea2cc!important; } #ptmp-monthly-sales-summary .ptmp-products-list a strong { font-size: 14px; font-weight: 400; display: block; color: #21759b; } |
Upon the successful execution of the code mentioned above, you will be able to see the following final output on your Dashboard as shown in figure:
In our next article, you will learn to get the sales summary for custom monthly archives.
Written By: Abdullah Ramzan
Awesome work. But i am unable to edit products from above created widget.
Hello Zaheer,
Please visit the advanced version of the article where the product is linked to its admin edit permalink.
https://www.presstigers.com/getting-woocommerce-monthly-sales-archive-in-dashboard-widget/
Thanks for reaching us out. Let us know if you need any other assistance from us.
Regards,
Abdullah | WP Plugin Developer
Email: support@presstigers.com
Hi, guys!
¿How would I add a row with the total of this sales (cimplete orders)?
Regards anyway!
Antonio
Hello Antonio,
Thank you for writing to us.
Please visit the advanced version of the article.
Regards,
Thanks a lot! This helped me to find out how the get_order_report_data method works for a plugin I am just developing.
Regards
feli_x