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
- Have a shop with orders spread across multiple months
- Make sure some months have no orders
- Check the dashboard revenue total → it will be a multiple of the real value
Environment
- ProcessWire 3.x
- PWCommerce (current version)
Bug Report
Affected file
site/modules/traits/utilities/TraitPWCommerceUtilitiesOrder.phpDescription
Two bugs in
getMonthsOrdersRevenues()andgetThisYearsOrders()cause thedashboard to show wildly incorrect revenue totals (e.g. 8x the actual value).
Bug 1 –
$monthRevenueMoneynot reset between monthsIn
getMonthsOrdersRevenues(), the variable$monthRevenueMoneyis initialisedbefore the foreach loop. When a month has no orders, the
ifblock is skippedbut 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 loopso it resets for every month.
Bug 2 –
getThisYearsOrders()has no status filtergetThisYearsOrders()fetches ALL orders regardless of status. This meansabandoned 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!commentsacknowledging this was known but not yet implemented.
Fix: Add a status filter to the selector to only include completed orders:
Steps to reproduce
Environment