Skip to content

Commit 8304cd8

Browse files
committed
Add stats data helpers for average order value
1 parent 0420f6d commit 8304cd8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Factories/StatsV4DataHelper.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ final class StatsV4DataHelper {
3737
}
3838
}
3939

40+
/// Creates the text to display for the average order value.
41+
///
42+
static func createAverageOrderValueText(orderStatsData: OrderStatsData, currencyFormatter: CurrencyFormatter, currencyCode: String) -> String {
43+
if let value = averageOrderValue(orderStats: orderStatsData.stats) {
44+
// If order value is an integer, no decimal points are shown.
45+
let numberOfDecimals: Int? = value.isInteger ? 0 : nil
46+
return currencyFormatter.formatAmount(value, with: currencyCode, numberOfDecimals: numberOfDecimals) ?? String()
47+
} else {
48+
return Constants.placeholderText
49+
}
50+
}
51+
4052
// MARK: Views and Visitors Stats
4153

4254
/// Creates the text to display for the visitor count.
@@ -104,6 +116,16 @@ private extension StatsV4DataHelper {
104116
}
105117
}
106118

119+
/// Retrieves the average order value for the provided order stats.
120+
///
121+
static func averageOrderValue(orderStats: OrderStatsV4?) -> Decimal? {
122+
if let orderStats {
123+
return orderStats.totals.averageOrderValue
124+
} else {
125+
return nil
126+
}
127+
}
128+
107129
/// Retrieves the total revenue from the provided order stats and, optionally, a specific interval.
108130
///
109131
static func totalRevenue(at selectedIndex: Int?, orderStats: OrderStatsV4?, orderStatsIntervals: [OrderStatsV4Interval]) -> Decimal? {

0 commit comments

Comments
 (0)