Skip to content

Commit 10a3b3d

Browse files
committed
Update total weight and validation in package item to take locale into consideration
1 parent e8eed0c commit 10a3b3d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Package Details/Multi-package/ShippingLabelPackageItemViewModel.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@ final class ShippingLabelPackageItemViewModel: ObservableObject {
107107
let calculatedWeight = calculateTotalWeight(products: products,
108108
productVariations: productVariations,
109109
customPackage: packageListViewModel.selectedCustomPackage)
110-
110+
let localizedCalculatedWeight = NumberFormatter.localizedString(from: NSNumber(value: calculatedWeight)) ?? String(calculatedWeight)
111111
// Set total weight to initialTotalWeight if it's different from the calculated weight.
112112
// Otherwise use the calculated weight.
113113
if initialTotalWeight.isNotEmpty, initialTotalWeight != String(calculatedWeight) {
114114
isPackageWeightEdited = true
115115
totalWeight = initialTotalWeight
116116
} else {
117-
totalWeight = String(calculatedWeight)
117+
totalWeight = localizedCalculatedWeight
118118
}
119119

120120
$totalWeight
121-
.map { $0 != String(calculatedWeight) }
121+
.map { $0 != localizedCalculatedWeight }
122122
.assign(to: &$isPackageWeightEdited)
123123

124124
$totalWeight
@@ -222,7 +222,11 @@ private extension ShippingLabelPackageItemViewModel {
222222
/// Validate that total weight is a valid floating point number.
223223
///
224224
private func validateTotalWeight(_ totalWeight: String) -> Bool {
225-
totalWeight.isNotEmpty && Double(totalWeight) != 0 && Double(totalWeight) != nil
225+
guard totalWeight.isNotEmpty,
226+
let value = NumberFormatter.double(from: totalWeight) else {
227+
return false
228+
}
229+
return value > 0
226230
}
227231
}
228232

0 commit comments

Comments
 (0)