Skip to content

Commit a3dcd63

Browse files
authored
Merge pull request #6053 from woocommerce/issue/5946-order-items-subtotal
Order Creation: Update price label when product quantity changes
2 parents 946c406 + 7cb3afc commit a3dcd63

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Creation/ProductsSection/ProductRowViewModel.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
5151

5252
/// Label showing product details: stock status, price, and variations (if any).
5353
///
54-
lazy var productDetailsLabel: String = {
54+
var productDetailsLabel: String {
5555
let stockLabel = createStockText()
5656
let priceLabel = createPriceText()
5757
let variationsLabel = createVariationsText()
5858

5959
return [stockLabel, priceLabel, variationsLabel]
6060
.compactMap({ $0 })
6161
.joined(separator: "")
62-
}()
62+
}
6363

6464
/// Label showing product SKU
6565
///
@@ -191,14 +191,14 @@ final class ProductRowViewModel: ObservableObject, Identifiable {
191191
}
192192
}
193193

194-
/// Create the price text based on a product's price.
194+
/// Create the price text based on a product's price and quantity.
195195
///
196196
private func createPriceText() -> String? {
197197
guard let price = price else {
198198
return nil
199199
}
200-
let unformattedPrice = price.isNotEmpty ? price : "0"
201-
return currencyFormatter.formatAmount(unformattedPrice)
200+
let productSubtotal = quantity * (currencyFormatter.convertToDecimal(from: price)?.decimalValue ?? Decimal.zero)
201+
return currencyFormatter.formatAmount(productSubtotal)
202202
}
203203

204204
/// Create the variations text for a variable product.

WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Creation/ProductRowViewModelTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,21 @@ class ProductRowViewModelTests: XCTestCase {
129129
"Expected label to contain \"\(expectedPriceLabel)\" but actual label was \"\(viewModel.productDetailsLabel)\"")
130130
}
131131

132+
func test_view_model_updates_price_label_when_quantity_changes() {
133+
// Given
134+
let product = Product.fake().copy(price: "2.50")
135+
let currencyFormatter = CurrencyFormatter(currencySettings: CurrencySettings()) // Defaults to US currency & format
136+
137+
// When
138+
let viewModel = ProductRowViewModel(product: product, canChangeQuantity: false, currencyFormatter: currencyFormatter)
139+
viewModel.incrementQuantity()
140+
141+
// Then
142+
let expectedPriceLabel = "$5.00"
143+
XCTAssertTrue(viewModel.productDetailsLabel.contains(expectedPriceLabel),
144+
"Expected label to contain \"\(expectedPriceLabel)\" but actual label was \"\(viewModel.productDetailsLabel)\"")
145+
}
146+
132147
func test_view_model_creates_expected_product_details_label_for_variable_product() {
133148
// Given
134149
let product = Product.fake().copy(productTypeKey: "variable", stockStatusKey: "instock", variations: [0, 1])

0 commit comments

Comments
 (0)