Skip to content

Missing ProductDetails.priceLocale #1013

Description

@NachoSoto

SK2's Product has localizedPriceString, but some users (including me) might need to calculate other prices, like "cost per month" for their UI. This is my app for example:

Screen Shot 2021-11-30 at 12 50 01

I used to calculate that like this on version 3.x:

private extension Purchases.Package {
    var pricePerMonth: NSDecimalNumber {
        let periodsPerMonth: Int = {
            switch self.product.subscriptionPeriod?.unit {
            case .day: return 30
            case .week: return 4
            case .month: return 1
            case .year: return 12
            default: return 1
            }
        }() / (self.product.subscriptionPeriod?.numberOfUnits ?? 1)
        
        return self.product.price.dividing(by: NSDecimalNumber(value: periodsPerMonth))
    }
    
    var localizedPricePerMonth: String {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        formatter.locale = self.product.priceLocale

        return formatter.string(from: self.pricePerMonth)! + " " + Strings.Dates.Unit.perMonth
    }
}

Luckily the locale is part of the jsonRepresentation of an SK2 Product.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions