• Hello, I am trying to create the code that shows the total number of orders and total sales amount in woocommerce, but when I run the code below, I cannot get the correct result.

    What I want is to show all order numbers and total amounts created on the orders page. However, this code continues to show even if I delete an order, and the total sales amount is not correct. So, on the orders page, I need to show the number and total amount of orders that have not been deleted.

    function wc_get_order_count() {
    $args = array(
    'post_type' => 'shop_order',
    'post_status' => array_keys(wc_get_order_statuses()),
    'numberposts' => -1,
    );
    $orders = get_posts($args);
    return count($orders);
    }
    function wc_get_total_sales() {
    global $wpdb;

    $total_sales = $wpdb->get_var("
    SELECT SUM(meta.meta_value)
    FROM {$wpdb->prefix}postmeta AS meta
    LEFT JOIN {$wpdb->prefix}posts AS posts ON meta.post_id = posts.ID
    WHERE meta.meta_key = '_order_total'
    AND posts.post_type = 'shop_order'
    AND posts.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
    ");

    return $total_sales;

Viewing 1 replies (of 1 total)
  • Hi there @thecreator01 ,
    Helping out with custom coding of this nature is outside the scope of support that our support staff can help out with here, although I would recommend the following:

    • Running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code;
    • Checking whether there are existing plugins in the WordPress plugin repository that might be doing that already.
    • Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions): https://woo.com/community-slack/

    Hope it helps!

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.