Skip to content

Commit cc86183

Browse files
authored
Merge pull request #591 from woocommerce/fix/358-item-price
Reapply updates from "item price" PR (#577)
2 parents c0139d6 + b80f22c commit cc86183

File tree

13 files changed

+118
-25
lines changed

13 files changed

+118
-25
lines changed

Networking/Networking/Model/OrderItem.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public struct OrderItem: Decodable {
88
public let name: String
99
public let productID: Int
1010
public let quantity: Int
11+
public let price: NSDecimalNumber
1112
public let sku: String?
1213
public let subtotal: String
1314
public let subtotalTax: String
@@ -18,11 +19,12 @@ public struct OrderItem: Decodable {
1819

1920
/// OrderItem struct initializer.
2021
///
21-
public init(itemID: Int, name: String, productID: Int, quantity: Int, sku: String, subtotal: String, subtotalTax: String, taxClass: String, total: String, totalTax: String, variationID: Int) {
22+
public init(itemID: Int, name: String, productID: Int, quantity: Int, price: NSDecimalNumber, sku: String?, subtotal: String, subtotalTax: String, taxClass: String, total: String, totalTax: String, variationID: Int) {
2223
self.itemID = itemID
2324
self.name = name
2425
self.productID = productID
2526
self.quantity = quantity
27+
self.price = price
2628
self.sku = sku
2729
self.subtotal = subtotal
2830
self.subtotalTax = subtotalTax
@@ -31,6 +33,39 @@ public struct OrderItem: Decodable {
3133
self.totalTax = totalTax
3234
self.variationID = variationID
3335
}
36+
37+
/// The public initializer for OrderItem.
38+
///
39+
public init(from decoder: Decoder) throws {
40+
let container = try decoder.container(keyedBy: CodingKeys.self)
41+
let itemID = try container.decode(Int.self, forKey: .itemID)
42+
let name = try container.decode(String.self, forKey: .name)
43+
let productID = try container.decode(Int.self, forKey: .productID)
44+
let quantity = try container.decode(Int.self, forKey: .quantity)
45+
let decimalPrice = try container.decodeIfPresent(Decimal.self, forKey: .price) ?? Decimal(0)
46+
let price = NSDecimalNumber(decimal: decimalPrice)
47+
let sku = try container.decodeIfPresent(String.self, forKey: .sku)
48+
let subtotal = try container.decode(String.self, forKey: .subtotal)
49+
let subtotalTax = try container.decode(String.self, forKey: .subtotalTax)
50+
let taxClass = try container.decode(String.self, forKey: .taxClass)
51+
let total = try container.decode(String.self, forKey: .total)
52+
let totalTax = try container.decode(String.self, forKey: .totalTax)
53+
let variationID = try container.decode(Int.self, forKey: .variationID)
54+
55+
// initialize the struct
56+
self.init(itemID: itemID,
57+
name: name,
58+
productID: productID,
59+
quantity: quantity,
60+
price: price,
61+
sku: sku,
62+
subtotal: subtotal,
63+
subtotalTax: subtotalTax,
64+
taxClass: taxClass,
65+
total: total,
66+
totalTax: totalTax,
67+
variationID: variationID)
68+
}
3469
}
3570

3671

@@ -43,6 +78,7 @@ private extension OrderItem {
4378
case name = "name"
4479
case productID = "product_id"
4580
case quantity = "quantity"
81+
case price = "price"
4682
case sku = "sku"
4783
case subtotal = "subtotal"
4884
case subtotalTax = "subtotal_tax"
@@ -62,6 +98,7 @@ extension OrderItem: Comparable {
6298
lhs.name == rhs.name &&
6399
lhs.productID == rhs.productID &&
64100
lhs.quantity == rhs.quantity &&
101+
lhs.price == rhs.price &&
65102
lhs.sku == rhs.sku &&
66103
lhs.subtotal == rhs.subtotal &&
67104
lhs.subtotalTax == rhs.subtotalTax &&

Networking/NetworkingTests/Mapper/OrderListMapperTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class OrderListMapperTests: XCTestCase {
8787
XCTAssertEqual(firstItem.itemID, 890)
8888
XCTAssertEqual(firstItem.name, "Fruits Basket (Mix & Match Product)")
8989
XCTAssertEqual(firstItem.productID, 52)
90-
XCTAssertEqual(firstItem.quantity, 1)
90+
XCTAssertEqual(firstItem.quantity, 2)
91+
XCTAssertEqual(firstItem.price, NSDecimalNumber(integerLiteral: 30))
9192
XCTAssertEqual(firstItem.sku, "")
9293
XCTAssertEqual(firstItem.subtotal, "50.00")
9394
XCTAssertEqual(firstItem.subtotalTax, "2.00")

Networking/NetworkingTests/Mapper/OrderMapperTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class OrderMapperTests: XCTestCase {
7878
XCTAssertEqual(firstItem.itemID, 890)
7979
XCTAssertEqual(firstItem.name, "Fruits Basket (Mix & Match Product)")
8080
XCTAssertEqual(firstItem.productID, 52)
81-
XCTAssertEqual(firstItem.quantity, 1)
81+
XCTAssertEqual(firstItem.quantity, 2)
82+
XCTAssertEqual(firstItem.price, NSDecimalNumber(integerLiteral: 30))
8283
XCTAssertEqual(firstItem.sku, "")
8384
XCTAssertEqual(firstItem.subtotal, "50.00")
8485
XCTAssertEqual(firstItem.subtotalTax, "2.00")

Networking/NetworkingTests/Responses/order.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"name": "Fruits Basket (Mix & Match Product)",
8787
"product_id": 52,
8888
"variation_id": 0,
89-
"quantity": 1,
89+
"quantity": 2,
9090
"tax_class": "",
9191
"subtotal": "50.00",
9292
"subtotal_tax": "2.00",
@@ -123,7 +123,7 @@
123123
],
124124
"meta_data": [],
125125
"sku": "5555-A",
126-
"price": 0
126+
"price": 0.00
127127
}
128128
],
129129
"tax_lines": [

Networking/NetworkingTests/Responses/orders-load-all.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787
"name": "Fruits Basket (Mix & Match Product)",
8888
"product_id": 52,
8989
"variation_id": 0,
90-
"quantity": 1,
90+
"quantity": 2,
91+
"price": 30.00,
9192
"tax_class": "",
9293
"subtotal": "50.00",
9394
"subtotal_tax": "2.00",
@@ -101,8 +102,7 @@
101102
}
102103
],
103104
"meta_data": [],
104-
"sku": "",
105-
"price": 30
105+
"sku": ""
106106
},
107107
{
108108
"id": 891,

Storage/Storage/Model/MIGRATIONS.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
This file documents changes in the WCiOS Storage data model. Please explain any changes to the data model as well as any custom migrations.
44

5-
## Model 9 (Release 0.15)
5+
## Model 9 (Release 1.0.0.1)
6+
- @bummytime 2019-01-11
7+
- Added `price` attribute on `OrderItem` entity
8+
9+
Note: the 1.0.0 model 9 never made it to our users so we are not reving the version #.
10+
11+
## Model 9 (Release 1.0.0)
612
- @jleandroperez 2018-12-26
713
- New `Order.exclusiveForSearch` property
814
- New `OrderSearchResults` entity

Storage/Storage/Model/OrderItem+CoreDataProperties.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extension OrderItem {
1111
@NSManaged public var itemID: Int64
1212
@NSManaged public var name: String?
1313
@NSManaged public var quantity: Int64
14+
@NSManaged public var price: NSDecimalNumber?
1415
@NSManaged public var productID: Int64
1516
@NSManaged public var sku: String?
1617
@NSManaged public var subtotal: String?

Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 9.xcdatamodel/contents

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<entity name="OrderItem" representedClassName="OrderItem" syncable="YES">
8383
<attribute name="itemID" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
8484
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
85+
<attribute name="price" optional="YES" attributeType="Decimal" defaultValueString="0.0" syncable="YES"/>
8586
<attribute name="productID" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
8687
<attribute name="quantity" optional="YES" attributeType="Integer 64" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
8788
<attribute name="sku" optional="YES" attributeType="String" syncable="YES"/>
@@ -184,12 +185,12 @@
184185
</entity>
185186
<elements>
186187
<element name="Account" positionX="-200.9765625" positionY="63.5625" width="128" height="120"/>
187-
<element name="Note" positionX="-162" positionY="180" width="128" height="285"/>
188+
<element name="Note" positionX="-369.61328125" positionY="-95.85546875" width="128" height="285"/>
188189
<element name="Order" positionX="-20" positionY="27" width="128" height="720"/>
189190
<element name="OrderCoupon" positionX="-206.01953125" positionY="379.74609375" width="128" height="120"/>
190-
<element name="OrderItem" positionX="-364.890625" positionY="379.453125" width="128" height="225"/>
191+
<element name="OrderItem" positionX="-364.890625" positionY="379.453125" width="128" height="240"/>
191192
<element name="OrderNote" positionX="-365.16796875" positionY="630.2109375" width="128" height="135"/>
192-
<element name="OrderSearchResults" positionX="-153" positionY="189" width="128" height="75"/>
193+
<element name="OrderSearchResults" positionX="144.44140625" positionY="743.43359375" width="128" height="75"/>
193194
<element name="OrderStats" positionX="137.859375" positionY="388.33203125" width="128" height="225"/>
194195
<element name="OrderStatsItem" positionX="321.74609375" positionY="425.51171875" width="128" height="330"/>
195196
<element name="Site" positionX="-203.6875" positionY="208.0703125" width="128" height="150"/>

WooCommerce/Classes/Tools/Currency/CurrencyFormatter.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public class CurrencyFormatter {
7878
return nil
7979
}
8080

81+
return formatAmount(decimalAmount, with: currency)
82+
}
83+
84+
/// Applies currency option settings to the amount for the given currency.
85+
/// - Parameters:
86+
/// - amount: a NSDecimalNumber representation of the amount, from the API, with no formatting applied. e.g. "19.87"
87+
/// - currency: a 3-letter country code for currencies that are supported in the API. e.g. "USD"
88+
///
89+
func formatAmount(_ decimalAmount: NSDecimalNumber, with currency: String = CurrencySettings.shared.currencyCode.rawValue) -> String? {
8190
// Grab the read-only currency options. These are set by the user in Site > Settings.
8291
let separator = CurrencySettings.shared.decimalSeparator
8392
let position = CurrencySettings.shared.numberOfDecimals

WooCommerce/Classes/ViewModels/OrderItemViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct OrderItemViewModel {
3939
}
4040

4141
let itemTotal = currencyFormatter.formatAmount(item.total, with: currency) ?? String()
42-
let itemSubtotal = currencyFormatter.formatAmount(item.subtotal, with: currency) ?? String()
42+
let itemSubtotal = currencyFormatter.formatAmount(item.price, with: currency) ?? String()
4343

4444
return itemTotal + " (" + itemSubtotal + " × " + quantity + ")"
4545
}

0 commit comments

Comments
 (0)