Skip to content

Bug: Dashboard revenue stats incorrect due to missing monthly reset and missing status filter #26

@eutervogel

Description

@eutervogel

Bug Report

Affected file

site/modules/traits/utilities/TraitPWCommerceUtilitiesOrder.php

Description

Two bugs in getMonthsOrdersRevenues() and getThisYearsOrders() cause the
dashboard to show wildly incorrect revenue totals (e.g. 8x the actual value).


Bug 1 – $monthRevenueMoney not reset between months

In getMonthsOrdersRevenues(), the variable $monthRevenueMoney is initialised
before the foreach loop. When a month has no orders, the if block is skipped
but the old value from the previous month is still written into the result array.
This means months without orders inherit the revenue of the last active month,
causing the year total to be massively inflated.

Fix: Move $monthRevenueMoney = $this->money(0); inside the foreach loop
so it resets for every month.

// BEFORE (buggy)
$monthRevenueMoney = $this->money(0);
foreach ($months as $monthName) {
    $isInitialMonthRevenue = true;
    ...

// AFTER (fixed)
foreach ($months as $monthName) {
    $monthRevenueMoney = $this->money(0);
    $isInitialMonthRevenue = true;
    ...

Bug 2 – getThisYearsOrders() has no status filter

getThisYearsOrders() fetches ALL orders regardless of status. This means
abandoned checkouts (1001), drafts (1000) and cancelled orders (2000) are all
included in revenue calculations.

The code already has two // TODO WILL NEED TO ADD STATUS COMPLETE! comments
acknowledging this was known but not yet implemented.

Fix: Add a status filter to the selector to only include completed orders:

// BEFORE (buggy)
$selector = "include=all,check_access=0,template=" . PwCommerce::ORDER_TEMPLATE_NAME 
    . ",created>$endOfLastYearTimestamp,created<$startOfNextYearTimestamp,status<" 
    . Page::statusTrash;

// AFTER (fixed)
$selector = "include=all,check_access=0,template=" . PwCommerce::ORDER_TEMPLATE_NAME 
    . ",created>$endOfLastYearTimestamp,created<$startOfNextYearTimestamp,status<" 
    . Page::statusTrash . ",pwcommerce_order.status=" . PwCommerce::ORDER_STATUS_COMPLETED;

Steps to reproduce

  1. Have a shop with orders spread across multiple months
  2. Make sure some months have no orders
  3. Check the dashboard revenue total → it will be a multiple of the real value

Environment

  • ProcessWire 3.x
  • PWCommerce (current version)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions