Skip to content

Commit 1e6becf

Browse files
committed
Configure total weight for package item
1 parent 9264226 commit 1e6becf

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ final class ShippingLabelPackageItemViewModel: ObservableObject {
4343
///
4444
let weightUnit: String?
4545

46+
/// Whether the user has edited the total package weight. If true, we won't make any automatic changes to the total weight.
47+
///
48+
@Published private var isPackageWeightEdited: Bool = false
49+
4650
init(order: Order,
4751
orderItems: [OrderItem],
4852
packagesResponse: ShippingLabelPackagesResponse?,
@@ -65,18 +69,37 @@ final class ShippingLabelPackageItemViewModel: ObservableObject {
6569

6670
packageListViewModel.didSelectPackage(selectedPackageID)
6771
configureItemRows(products: products, productVariations: productVariations)
72+
configureTotalWeight(initialTotalWeight: totalWeight, products: products, productVariations: productVariations)
6873
}
6974

7075
private func configureItemRows(products: [Product], productVariations: [ProductVariation]) {
7176
itemsRows = generateItemsRows(products: products, productVariations: productVariations)
7277
}
78+
79+
/// Set value for total weight and observe its changes.
80+
///
81+
private func configureTotalWeight(initialTotalWeight: String, products: [Product], productVariations: [ProductVariation]) {
82+
let calculatedWeight = calculateTotalWeight(products: products, productVariations: productVariations, customPackage: packageListViewModel.selectedCustomPackage)
83+
84+
// Set total weight to initialTotalWeight if it's different from the calculated weight.
85+
// Otherwise use the calculated weight.
86+
if initialTotalWeight.isNotEmpty, initialTotalWeight != String(calculatedWeight) {
87+
isPackageWeightEdited = true
88+
totalWeight = initialTotalWeight
89+
} else {
90+
totalWeight = String(calculatedWeight)
91+
}
92+
93+
$totalWeight
94+
.map { $0 != String(calculatedWeight) }
95+
.assign(to: &$isPackageWeightEdited)
96+
}
7397
}
7498

7599
// MARK: ShippingLabelPackageSelectionDelegate conformance
76100
extension ShippingLabelPackageItemViewModel: ShippingLabelPackageSelectionDelegate {
77101
func didSelectPackage(id: String) {
78-
// TODO-4599: set to current total weight if manually edited
79-
let newTotalWeight = ""
102+
let newTotalWeight = isPackageWeightEdited ? totalWeight : ""
80103
let newPackage = ShippingLabelPackageAttributes(packageID: id,
81104
totalWeight: newTotalWeight,
82105
productIDs: orderItems.map { $0.productOrVariationID })
@@ -124,6 +147,40 @@ private extension ShippingLabelPackageItemViewModel {
124147
}
125148
return itemsToFulfill
126149
}
150+
151+
/// Calculate total weight based on the weight of the selected package if it's a custom package;
152+
/// And the products and products variation inside the order items, only if they are not virtual products.
153+
///
154+
/// Note: Only custom package is needed for input because only custom packages have weight to be included in the total weight.
155+
///
156+
func calculateTotalWeight(products: [Product], productVariations: [ProductVariation], customPackage: ShippingLabelCustomPackage?) -> Double {
157+
var tempTotalWeight: Double = 0
158+
159+
// Add each order item's weight to the total weight.
160+
for item in orderItems {
161+
let isVariation = item.variationID > 0
162+
var product: Product?
163+
var productVariation: ProductVariation?
164+
165+
if isVariation {
166+
productVariation = productVariations.first { $0.productVariationID == item.variationID }
167+
}
168+
else {
169+
product = products.first { $0.productID == item.productID }
170+
}
171+
if product?.virtual == false || productVariation?.virtual == false {
172+
let itemWeight = Double(productVariation?.weight ?? product?.weight ?? "0") ?? 0
173+
tempTotalWeight += itemWeight * Double(truncating: item.quantity as NSDecimalNumber)
174+
}
175+
}
176+
177+
// Add selected package weight to the total weight.
178+
// Only custom packages have a defined weight, so we only do this if a custom package is selected.
179+
if let selectedPackage = customPackage {
180+
tempTotalWeight += selectedPackage.boxWeight
181+
}
182+
return tempTotalWeight
183+
}
127184
}
128185

129186
private extension ShippingLabelPackageItemViewModel {

0 commit comments

Comments
 (0)