Skip to content

Commit 7a07874

Browse files
committed
Add support to update product quantities
1 parent 32077ba commit 7a07874

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Creation/NewOrderViewModel.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,10 @@ private extension NewOrderViewModel {
466466
// Observe changes to the product quantity
467467
productRowViewModel.$quantity
468468
.sink { [weak self] newQuantity in
469-
// TODO: Add update quantity support
470-
// self?.orderDetails.items[index].quantity = newQuantity
469+
guard let self = self, let newInput = self.createUpdateProductInput(item: item, quantity: newQuantity) else {
470+
return
471+
}
472+
self.orderSynchronizer.setProduct.send(newInput)
471473
}
472474
.store(in: &cancellables)
473475

@@ -553,6 +555,32 @@ private extension NewOrderViewModel {
553555
}
554556
return OrderSyncAddressesInput(billing: billingAddress, shipping: shippingAddress)
555557
}
558+
559+
/// Creates a new `OrderSyncProductInput` type meant to update an existing input from `OrderSynchronizer`
560+
/// If the referenced product can't be found, `nil` is returned.
561+
///
562+
private func createUpdateProductInput(item: OrderItem, quantity: Decimal) -> OrderSyncProductInput? {
563+
// Finds the product or productVariation associated with the order item.
564+
let product: OrderSyncProductInput.ProductType? = {
565+
if item.variationID != 0, let variation = allProductVariations.first(where: { $0.productVariationID == item.variationID }) {
566+
return .variation(variation)
567+
}
568+
569+
if let product = allProducts.first(where: { $0.productID == item.productID }) {
570+
return .product(product)
571+
}
572+
573+
return nil
574+
}()
575+
576+
guard let product = product else {
577+
DDLogError("⛔️ Product with ID: \(item.productID) not found.")
578+
return nil
579+
}
580+
581+
// Return a new input with the new quantity but with the same item id to properly reference the update.
582+
return OrderSyncProductInput(id: item.itemID, product: product, quantity: quantity)
583+
}
556584
}
557585

558586
private extension NewOrderViewModel {

0 commit comments

Comments
 (0)