Total number of monthly sales Woocommerce shortcode

This code defines a function total_sales_this_month() that calculates the total number of completed sales for the current month. It uses a custom query to count the number of completed orders within the specified date range.

After adding this code, you can use the [total_sales_this_month] shortcode in your WordPress content to display the total number of sales this month wherever you want.

    
function total_sales_this_month() { // Get the current date $current_date = date('Y-m-d'); // Calculate the first day of the current month $first_day_of_month = date('Y-m-01'); // Create a query to get the total sales for this month $args = array( 'post_type' => 'shop_order', 'post_status' => 'wc-completed', 'date_query' => array( 'after' => $first_day_of_month, 'before' => $current_date, ), 'fields' => 'ids', ); $query = new WP_Query($args); // Get the total number of sales $total_sales = $query->found_posts; // Output the total sales return $total_sales; } add_shortcode('total_sales_this_month', 'total_sales_this_month');

Leave a Reply